DevT DevT - 7 months ago 83
C# Question

how convert unix timestamp to datetime

I'm trying to convert this unix timestamp
in DateTime, in this way:

private static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp);
return dtDateTime;

But I get a wrong date:
Date: {04/11/0045 00:00:00}

NOTE: dtDateTime.AddSeconds(unixTimeStamp) throws an exception.. my number is in Milliseconds.

with this online tool the conversion is right:

04/11/2014 15:35:03 GMT+0:00

How I can convert this one?


Your code is working just fine, as is. Here is a fiddle.

Everyone that is telling you to use AddSeconds is wrong. The number you are giving us is clearly in milliseconds. There are 31,536,000 seconds in a year. 1415115303410 divided by 31536000 is 4487. There hasn't been 4,487 years passed since 1/1/1970.