I must be doing an obvious mistake but I can't figure it out.
I am importing a date stored in a mysql database (it is stored by the ExpressionEngine CMS). It is a unix timestamp, i.e. seconds since 1/1/1970 00:00.
So I'm doing something like this:
DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(orderdate /* int read from the database */);
Unfortunately I don't get the right result. Here is an example:
Value read from the DB: 1258598728 (this is an order date)
Paypal sent an email establishing the order at Nov 18, 2009 12:45:20 PST
The php web site that reads this value in the DB and knows how to display this date correctly displays it as 2009-11-18 03:45 PM (which seems correct since I’m hosted at a server on the east coast)
My code above gives 11/19/2009 2:45:28 AM !! (UTC which gives 11/18/2009 9:45 PM east time, i.e. 6 hours difference with what is expected)
I get the same result if using DateTimeOffset taking care of putting the right timezone.
Any idea what I'm doing wrong?
Try this:
DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
DateTime myDate = epoch.AddSeconds(1258598728).toLocalTime();
http://www.onlineconversion.com/unix_time.htm confirms your calculations are right. Unix time 1258598728 = "Thu, 19 Nov 2009 02:45:28 GMT"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With