DevT DevT - 18 days ago 5
C# Question

how convert unix timestamp to datetime

I'm trying to convert this unix timestamp
1415115303410
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: http://www.epochconverter.com/

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

How I can convert this one?

Answer

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.