Yang
Yang

Reputation: 6912

javascript Date.parse and Date.UTC give different results

Can anyone explain to me why?

d = Date.parse('8/15/2012 '+'11:59:45 AM');
alert(d);
alert(Date.UTC(2012, 7, 15, 11, 59, 45));

Upvotes: 4

Views: 11397

Answers (2)

Corbin
Corbin

Reputation: 33467

Date.parse assumes local time if not specified.

The UTC one, however, is obviously UTC.

For example, my computer is UTC -5 (well, Chicago CDT actually), so the two timestamps happen to be 5 hours apart for me.

You will get the same thing if you specify UTC:

Date.parse('8/15/2012 '+'11:59:45 AM UTC'); //1345031985000
Date.UTC(2012, 7, 15, 11, 59, 45); //1345031985000

Upvotes: 7

Euric
Euric

Reputation: 359

I'll assume the difference in months was a typo in your question.

Date.parse returns the difference between the date provided and midnight 1 January 1970.

Date.UTC returns the difference between your date and midnight 1 January 1970 GMT.

If your timezone is set to GMT (UTC) you should expect to see the same value returned by both calls.

Upvotes: -1

Related Questions