How does javascript know the time zone of a date in milliseconds?

北慕城南 提交于 2019-12-11 04:18:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!