问题
The following code when executed using w3schools' interactive js environment (here):
var d1=new Date(1306796400000);
document.write("Original form: " + d1);
displays the following message:
Original form: Tue May 31 2011 00:00:00 GMT+0100 (GMT Daylight Time)
But this:
var d1=new Date(1231977600000);
document.write("Original form: " + d1);
displays this message:
Original form: Thu Jan 15 2009 00:00:00 GMT+0000 (GMT Standard Time)
I thought that the millisecond value was just milliseconds since 01/01/1970 in UTC. But it looks like it contains a flag for time zone.
Can anyone say what the millisecond value format is?
Thanks in advance.
回答1:
There is no special flag. It's just Daylight Savings in effect.
回答2:
Javascript is executed inside of the user's browser, which in turn, reads the current time zone from user's OS. That's how it can "guess" the proper time zone.
回答3:
The milliseconds do not contain any such flag. However, the time zone of the date object you create using new Date(n)
is dependent upon the locale in your interpreter/browser. For me:
var d = new Date(1231977600000);
d.toString();
// "Wed Jan 14 2009 17:00:00 GMT-0700 (Mountain Standard Time)"
d.toUTCString();
// "Thu, 15 Jan 2009 00:00:00 GMT"
回答4:
It does not. It uses local timezone information, including DST transition date. Hence the difference in
javascript:alert([new Date(1306796400000),new Date(1231977600000)].join('\n'))
Set your locale to DST-less timezone and the difference will disappear.
来源:https://stackoverflow.com/questions/4379922/how-does-javascript-know-the-time-zone-of-a-date-in-milliseconds