Sunday, August 11, 2013

Convert JavaScript Epoch Milliseconds to C# DateTime

// Passed into the service; this example is UTC 1/1/2012, so it'll end up being 5:00am Eastern
string millisecondsFromEpochJavaScript = "1325394000000";

// Start at 1/1/1970 since that is considered the epoch date.
// Multiply by 10000 to convert milliseconds to "ticks", which are 100 nanoseconds.
DateTime converted = new DateTime(1970, 1, 1).AddTicks(Convert.ToInt64(millisecondsFromEpochJavaScript) * 10000);