Skip to content

Having a UNIX TimeStamp Converter in C#

May 4, 2012

A nice database feature is to have a UNIX timestamp rather than a datetime object inside a SQL database, perhaps it can read the database record quicker rather than reading the individual components of a data aswell as for indexing, I need to write a proper blog on analysing this at a later date.

Anyways using JQPlot to ustilise the UNIXTimeStamp on the dates: – 

I wrote the following code to convert a UNIX TImestamp into human readable format for use for the JQPlot graph, the second one was my preferred choice as it converts a string to a date time format string for sheer ease of use.

public static long GetJavascriptTimestamp(DateTime input)
TimeSpan span = new TimeSpan(DateTime.Parse(“1/1/1970”).Ticks);
DateTime time = input.Subtract(span);
return (time.Ticks / 10000);

public static string DateTimeFromJavascript(long millisecs)
return new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(millisecs).ToString(“MM-dd-yyyy hh:mm:ss”);

For the C# method to produce the correct output for the JQPlot, I simply wrote the method which utilises the above: –

public static List<DatePoints> GetData2()
if (_datepoints == null)
_datepoints = new List<DatePoints>();

IList<GraphData> graphData = GraphData.GetGraph();
foreach (GraphData graphDataItem in graphData)
string d = GraphData.DateTimeFromJavascript(graphDataItem.XAxisInterval).ToString();
_datepoints.Add(new DatePoints(GraphData.DateTimeFromJavascript(graphDataItem.XAxisInterval).ToString(), Convert.ToDouble(graphDataItem.YAxisValue)));
return _datepoints;

GraphData object is my own customised C# object which holds the GraphData as an object holding static methods aswell as the object DatePoints which holds a key and value object, which key is string for the date, and value is for the integer value all used for JQPlot.



From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: