Date.prototype.getTimezoneOffset()
Date
实例的 getTimezoneOffset()
方法返回在 UTC 时区中计算的该日期与在本地时区中计算的同一日期之间的差异(以分钟为单位)。
¥The getTimezoneOffset()
method of Date
instances returns the difference, in minutes, between this date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.
Try it
语法
参数
返回值
¥Return value
表示在 UTC 时区中计算的日期与在本地时区中计算的日期之间的差异(以分钟为单位)的数字。实际的本地时间算法是实现定义的,并且在没有适当数据的运行时允许返回值为零。如果日期是 invalid,则返回 NaN
。
¥A number representing the difference, in minutes, between the date as evaluated in the UTC time zone and as evaluated in the local time zone. The actual local time algorithm is implementation-defined, and the return value is allowed to be zero in runtimes without appropriate data. Returns NaN
if the date is invalid.
描述
¥Description
date.getTimezoneOffset()
返回在 UTC 时区中计算的 date
与在本地时区中计算的 date
之间的差异(以分钟为单位),即使用浏览器的主机系统的时区(如果代码运行于 浏览器中的 Web),或者执行代码的任何 JavaScript 运行时的主机系统(例如 Node.js 环境)。
¥date.getTimezoneOffset()
returns the difference, in minutes, between date
as evaluated in the UTC time zone and as evaluated in the local time zone — that is, the time zone of the host system in which the browser is being used (if the code is run from the Web in a browser), or otherwise the host system of whatever JavaScript runtime (for example, a Node.js environment) the code is executed in.
负值和正值
¥Negative values and positive values
如果本地时区晚于 UTC,则 getTimezoneOffset()
返回的分钟数为正;如果本地时区早于 UTC,则 getTimezoneOffset()
返回的分钟数为负。例如,对于 UTC+10,将返回 -600
。
¥The number of minutes returned by getTimezoneOffset()
is positive if the local time zone is behind UTC, and negative if the local time zone is ahead of UTC. For example, for UTC+10, -600
will be returned.
当前时区 | 返回值 |
---|---|
UTC-8 | 480 |
UTC | 0 |
UTC+3 | -180 |
夏令时 (DST) 地区的不同结果
¥Varied results in Daylight Saving Time (DST) regions
在每年夏令时 (DST) 切换的地区,由于 date
不同,调用 getTimezoneOffset()
返回的分钟数可能会不一致。
¥In a region that annually shifts in and out of Daylight Saving Time (DST), as date
varies, the number of minutes returned by calling getTimezoneOffset()
can be non-uniform.
注意:
getTimezoneOffset()
的行为永远不会根据代码运行的时间而有所不同 - 在同一区域运行时,其行为始终保持一致。只有date
的值影响结果。¥Note:
getTimezoneOffset()
's behavior will never differ based on the time when the code is run — its behavior is always consistent when running in the same region. Only the value ofdate
affects the result.
注意:许多国家都尝试过每年两次不改变时间,这意味着 DST 也将持续整个冬季。例如,在英国,夏令时从 1968 年 2 月 18 日凌晨 2:00 持续到 1971 年 10 月 31 日凌晨 3:00,因此在冬季时钟不会调慢。
¥Note: Many countries have experimented with not changing the time twice a year and this has meant that DST has continued over the winter too. For example in the UK DST lasted from 2:00AM 18 February 1968 to 3:00AM 31 October 1971, so during the winter the clocks were not set back.
在大多数实现中,IANA 时区数据库(tzdata)用于精确确定 date
时刻本地时区的偏移量。但是,如果此类信息不可用,则实现可能会返回零。
¥In most implementations, the IANA time zone database (tzdata) is used to precisely determine the offset of the local timezone at the moment of the date
. However, if such information is unavailable, an implementation may return zero.
示例
使用 getTimezoneOffset()
¥Using getTimezoneOffset()
// Create a Date instance for the current time
const currentLocalDate = new Date();
// Create a Date instance for 03:24 GMT-0200 on May 1st in 2016
const laborDay2016at0324GMTminus2 = new Date("2016-05-01T03:24:00-02:00");
currentLocalDate.getTimezoneOffset() ===
laborDay2016at0324GMTminus2.getTimezoneOffset();
// true, always, in any timezone that doesn't annually shift in and out of DST
// false, sometimes, in any timezone that annually shifts in and out of DST
getTimezoneOffset() 和 DST
¥getTimezoneOffset() and DST
在使用 DST 的地区,返回值可能会根据 date
所在年份的时间而变化。以下是纽约运行时的输出,其中时区为 UTC-05:00。
¥In regions that use DST, the return value may change based on the time of the year date
is in. Below is the output in a runtime in New York, where the timezone is UTC-05:00.
const nyOffsetSummer = new Date("2022-02-01").getTimezoneOffset(); // 300
const nyOffsetWinter = new Date("2022-08-01").getTimezoneOffset(); // 240
getTimezoneOffset() 和历史数据
¥getTimezoneOffset() and historical data
由于历史原因,一个地区所在的时区可能会不断变化,甚至不考虑夏令时。例如,以下是上海运行时的输出,其中时区为 UTC+08:00。
¥Due to historical reasons, the timezone a region is in can be constantly changing, even disregarding DST. For example, below is the output in a runtime in Shanghai, where the timezone is UTC+08:00.
const shModernOffset = new Date("2022-01-27").getTimezoneOffset(); // -480
const shHistoricalOffset = new Date("1943-01-27").getTimezoneOffset(); // -540
这是因为在 甲午战争 期间,上海在日本的控制下,时区被更改为 UTC+09:00 以与日本保持一致(实际上,这是 "全年夏令时"),并且这被记录在 IANA 数据库中。
¥This is because during the Sino-Japanese War when Shanghai was under Japanese control, the timezone was changed to UTC+09:00 to align with Japan's (in effect, it was a "year-round DST"), and this was recorded in the IANA database.
规范
Specification |
---|
ECMAScript Language Specification # sec-date.prototype.gettimezoneoffset |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also