JavaScript 时区偏移量不匹配 Windows 偏移量

JavaScript Time Zone Offset does not match Windows Offset

我在使用 JavaScript 时遇到了一个独特的问题。当 运行 以下代码时,我的系统处于时区 UTC -5 Eastern Standard Time,我得到的值为 -4 UTC,它应该是 -5 UTC

目前已启用夏令时,但 JavaScript 似乎没有意识到这一点。这里有什么问题,还有其他方法可以检索正确的偏移量吗?

var d = new Date();
var tz = d.toString().split("GMT")[1].split(" (")[0];
alert(tz);

我系统的时间和时区:

现在 (2015 年 4 月 8 日) EST – Eastern Standard Time (-05:00 UTC) is not in effect, however, EDT – Eastern Daylight Time 是 (-04:00 UTC)。 return 由 JavaScript 日期对象编辑的值是正确的。

日期和时间对话框中的 "(UTC-05:00) 东部时间(美国和加拿大)" 行具有误导性。该行未说明当前偏移量,仅说明 DST 未生效时的时区偏移量。

你也可以简化你的JavaScript,不需要解析toString()的return,实际上这被认为是不好的做法。

var offset = new Date().getTimezoneOffset();

document.write("UTC offset in minutes: " + offset 
               + ", offset in hours: " + offset / 60);


仅供参考。

无论如何我都不是日期和时间专家,但我通常发现将时区视为与 UTC 偏移不同的东西会很有帮助。

From Wikipedia

A time zone is a region that has a uniform standard time for legal, commercial, and social purposes. It is convenient for areas in close commercial or other communication to keep the same time, so time zones tend to follow the boundaries of countries and their subdivisions.

From Wikipedia

The UTC offset is the difference in hours and minutes from Coordinated Universal Time (UTC) for a particular place and date.

这个区别很重要,以加拿大萨斯喀彻温省为例

From Wikipedia

The Canadian province of Saskatchewan is geographically located in the Mountain Time Zone. However, most of the province observes Central Standard Time year-round. As a result, it is effectively on daylight saving time (DST) year round, as clocks are not turned back an hour in autumn.

所以这里是一个时区(Mountain Time Zone)同时具有多个UTC时差的例子。或者,如果您更喜欢萨斯喀彻温省,则从 3 月到 11 月属于山区时区,而在今年剩余时间属于中部时区。