views:

67

answers:

2

Given a time:

1286294501433

Which represents milliseconds passed since 1970, how do we convert this to a DateTime data type? EG:

transactionTime = "1286294501433";
UInt64 intTransTime = UInt64.Parse(transactionTime);
DateTime transactionActualDate = DateTime.Parse(intTransTime.ToString());

Throws:

String was not recognized as a valid DateTime.

Please note all times passed into this function are guaranteed to be after 1970.

+9  A: 
var dt = new DateTime(1970, 1, 1).AddMilliseconds(1286294501433);

You might also need to specify the DateTimeKind explicitly, depending on your exact requirements:

var dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
             .AddMilliseconds(1286294501433);
LukeH
Great answer thank you
Tom Gullen
A: 

Assuming this is unix time its the number of seconds, so

int unixtimestamp=int.Parse(str);
new DateTime(1970,1,1,0,0,0).AddSeconds(unixtimestamp);

like this fetcher guy said.

wiki says

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. I

Roman A. Taycher