Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 conversion tool http://www.epochconverter.com/ I get the right conversion:

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

How I can convert this one?

like image 598
DevT Avatar asked Feb 03 '15 17:02

DevT


1 Answers

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.

like image 114
Icemanind Avatar answered Oct 18 '22 16:10

Icemanind