views:

53

answers:

3

i have code that takes a csharp datetime and converts it into a long to plot in the "flot" graph. here is the code

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

I now need an opposite function where i take this long value and get the csharp datetime object back. any idea if the above method can be reversed ?

A: 

Can be:

public static DateTime GetTimestampFromJS(long ts)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(ts*1000);
}
halfdan
+1  A: 

Aren't you just looking for this?

public static DateTime DateTimeFromJavascript(long millisecs)
{
    return new DateTime(1970, 1, 1).AddMilliseconds(millisecs);
}
DocMax
+2  A: 
DateTime date = new DateTime(1970, 1, 1).Add(new TimeSpan(yourLong * 10000));
Anthony Pegram