Date.prototype.getTimezoneOffset() - JavaScript 编辑
The getTimezoneOffset()
method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
dateObj.getTimezoneOffset()
Return value
A number representing the time-zone offset, in minutes, from the date based on current host system settings to UTC.
Description
The time-zone offset is the difference, in minutes, from local time to UTC.
Note that this means that the offset is positive if the local timezone is behind UTC, and negative if it is ahead. For example, for time zone UTC+10:00 (Australian Eastern Standard Time, Vladivostok Time, Chamorro Standard Time), -600
will be returned.
Current Locale | UTC-8 | UTC | UTC+3 |
---|---|---|---|
Return Value | 480 | 0 | -180 |
The time zone offset returned is the one that applies for the Date that it's called on.
If the host system is configured for daylight saving, the offset will change depending on the date and time that the Date represents and that daylight saving applies.
Examples
Using getTimezoneOffset()
// Get current timezone offset for host device
let x = new Date();
let currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
// 1
// Get timezone offset for International Labour Day (May 1) in 2016
// Be careful, the Date() constructor uses 0-indexed months, so May is
// represented with 4 (and not 5)
let labourDay = new Date(2016, 4, 1)
let labourDayOffset = labourDay.getTimezoneOffset() / 60;
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Date.prototype.getTimezoneOffset' in that specification. |
Browser compatibility
BCD tables only load in the browser
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论