简体   繁体   English

getTimezoneOffset结果不一致

[英]Inconsistent getTimezoneOffset results

The documentation seems to suggest getTimezoneOffset() always returns the offset of the current locale, irregardless of the date object. 文档似乎建议getTimezoneOffset()始终返回当前语言环境的偏移量,无论日期对象如何。 But I'm getting inconsistent results that I can't understand. 但是我得到的结果不一致,我无法理解。

new Date().getTimezoneOffset()                             // -120
new Date("2015-03-10T15:48:05+01:00").getTimezoneOffset()  // -60
new Date("2015-03-10T15:48:05-04:00").getTimezoneOffset()  // -60

Also, is there a better way to get the timezone off a datetime string (maybe with moment.js)? 另外,是否有更好的方法可以将时区从日期时间字符串中删除(也许使用moment.js)?

getTimezoneOffset returns the offset for the specific moment in time represented by the Date object it is called on, using the time zone setting of the computer that it executing the code. getTimezoneOffset使用执行代码的计算机的时区设置,返回由调用它的Date对象表示的特定时刻的偏移量。

Since many time zones change their offset for daylight saving time , it is perfectly normal for the value to differ for different dates and times. 由于许多时区会更改其夏令时的偏移量,因此不同日期和时间的值不同是完全正常的。 When you call it on new Date() , you're getting the current offset. 当您在new Date()上调用它时,您将获得当前的偏移量。

The value returned from getTimezoneOffset is in terms of minutes west of UTC, as compared to the more common offsets returned in [+/-]HH:mm format, which are east of UTC. getTimezoneOffset返回的值以UTC 以西的分钟数表示,与以[+/-]HH:mm格式返回的更常见的偏移量相比,这些偏移量在UTC 以东 Therefore, the time zone you gave alternates between UTC+1, and UTC+2. 因此,您提供的时区在UTC + 1和UTC + 2之间交替显示。 My guess is the computer that gave this output was in one of the zones that uses Central European Time - though it could be one of several others. 我的猜测是,计算机提供的输出是在使用中欧时间的其中一个区域 - 尽管它可能是其他几个区域之一。

Also, when you pass in an offset as part of an ISO8601 formatted string, that offset is indeed taken into account - but only during parsing. 此外,当您将偏移量作为ISO8601格式化字符串的一部分传入时,确实会考虑该偏移量 - 但仅在解析期间。 The offset is applied, and the Date object holds on to the UTC timestamp internally. 应用偏移量, Date对象在内部保留UTC时间戳。 It then forgets anything about the offset you supplied. 然后它会忘记你提供的偏移量。 On output, some of the functions will explicitly use UTC, but most of them will convert to the local time zone before emitting their result. 在输出时,一些函数将明确使用UTC,但大多数函数将在发出结果之前转换为本地时区。

You also asked about how to get the offset of a datetime string using moment.js . 您还询问了如何使用moment.js获取日期时间字符串的偏移量。 Yes, that is quite simple: 是的,这很简单:

// Create a moment object.
// Use the parseZone function to retain the zone provided.
var m = moment.parseZone('2015-03-10T15:48:05-04:00');

// get the offset in minutes EAST of UTC (opposite of getTimezoneOffset)
var offset = m.utcOffset(); // -240

// alternatively, get it as a string in [+/-]HH:mm format
var offsetString = m.format("Z");  // "-04:00"

It's because of Daylight Savings Time . 这是因为夏令时 For your timezone, on June 11th it is in UTC+2 and on March 10th it's in UTC+1: 对于您的时区,6月11日是UTC + 2,3月10日是UTC + 1:

// when in DST (since it's June)
new Date("2015-06-11T00:00:00Z").getTimezoneOffset();       // -120
// when not in DST
new Date("2015-03-10T15:48:05+01:00").getTimezoneOffset();  // -60

For me, since I'm in the Eastern Time Zone, the following will happen: 对我来说,因为我在东部时区,所以会发生以下情况:

// when in EST
new Date("2015-03-01T00:00:00Z").getTimezoneOffset();       // 300
// when in EDT
new Date("2015-06-01T00:00:00Z").getTimezoneOffset();       // 240

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM