5

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?

3 Answers 3

9

Try this:

DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKind.Utc);
DateTime myDate = epoch.AddSeconds(1258598728).toLocalTime();
Sign up to request clarification or add additional context in comments.

3 Comments

It prints: 11/18/2009 9:45:28 PM here (GMT-4)
yes i'ts still the same value I get (6 hours difference with the expected value)
well, then it has to be a problem with the data.
1

http://www.onlineconversion.com/unix_time.htm confirms your calculations are right. Unix time 1258598728 = "Thu, 19 Nov 2009 02:45:28 GMT"

Comments

0

Your conversion is correct, the data is wrong. That's the simplest and most likely explanation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.