javascript 如何知道日期的时区(以毫秒为单位)?
使用 w3schools 的交互式 js 环境(此处)执行以下代码:
var d1=new Date(1306796400000);
document.write("Original form: " + d1);
显示以下消息:
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);
显示此消息:
Original form: Thu Jan 15 2009 00:00:00 GMT+0000 (GMT Standard Time)
我认为毫秒值只是自 UTC 1970 年 1 月 1 日以来的毫秒数。 但看起来它包含一个时区标志。
谁能说出毫秒值的格式是什么?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有特殊的标志。这只是夏令时生效。
There is no special flag. It's just Daylight Savings in effect.
JavaScript 在用户浏览器内部执行,浏览器又从用户操作系统读取当前时区。这就是它如何“猜测”正确的时区。
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.
毫秒不包含任何此类标志。但是,您使用
new Date(n)
创建的日期对象的时区取决于您的解释器/浏览器中的区域设置。为我: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:事实并非如此。它使用本地时区信息,包括 DST 转换日期。因此,将
您的区域设置设置为无 DST 时区的差异将消失。
It does not. It uses local timezone information, including DST transition date. Hence the difference in
Set your locale to DST-less timezone and the difference will disappear.