date.getTimezoneOffset() 未返回预期值

date.getTimezoneOffset() is not returning an expected value

我的浏览器在东部标准时区 运行,当我调用 date.getTimezoneOffset() 时,我希望返回 -300,但我得到的是 300

var date = new Date();
date.getTimezoneOffset();
// returns 300

来自 the Mozilla docs (or devdocs.io):

Return value
The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

EST 落后于 UTC,因此您得到了一个积极的结果。

我同意这不是表示它的好方法 - 我更习惯于 "the amount of time you add to UTC to get local time" 的偏移量,但它至少表现得像记录的那样...

这是从 UTC 到您所在时区的分钟差。UTC - EST = 300。

来自MDN对函数的参考描述:

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset