javascript - iso '' 0001-01-01T01 :00:00' adding 16 second 创建日期

标签 javascript datetime timezone-offset

new Date("0001-01-01T01:00:00Z") --> Mon Jan 01 0001 02:50:16 GMT+0150 (Moscow Standard Time)
GMT 不正确:我的时区 GMT+3000 , 但日期创建 GMT+0150

最佳答案

对于日期,您可以(而且我认为应该)以 UTC ISO 8601“Z”格式(“YYYY-MM-DDTHH:MM:SSZ”)定义它们,就像您所做的那样。

但是,要获得这些日期的用户友好的字符串表示,这将取决于您的客户端和所使用的 Javascript 引擎。如果您使用 toLocaleString() 明确指定引用时区,则可以限制输出。

    var date = new Date("1990-01-01T01:00:00Z");

    console.log(date.toLocaleString("en-US", {timeZone: "Asia/Jerusalem"}));
    console.log(date.toLocaleString("en-US", {timeZone: "Europe/Moscow"}));
    console.log(date.toLocaleString("en-US", {timeZone: "Africa/Djibouti"}));
    // output on my machine, should be the same on yours : 
    // 1/1/1990, 3:00:00 AM
    // 1/1/1990, 4:00:00 AM
    // 1/1/1990, 4:00:00 AM

    console.log(date.toString());
    // output **on my machine**, should **not** be the same on yours
    // Mon Jan 01 1990 02:00:00 GMT+0100 (Central European Standard Time)


对于 16 秒 问题,这与 IANA 时区概念存在之前那些日期的规则定义偏移量的方式有关。

它们在您的应用程序中可能没有意义,我不鼓励您使用 0001 年 1 月 1 日这样的日期作为示例。

例子:

    var date = new Date("0001-01-01T01:00:00Z");

    console.log(date.toLocaleString("en-US", {timeZone: "Asia/Jerusalem"}));
    console.log(date.toLocaleString("en-US", {timeZone: "Europe/Moscow"}));
    console.log(date.toLocaleString("en-US", {timeZone: "Africa/Djibouti"}));
    // output on my machine, should be the same on yours : 
    // 1/1/1, 3:20:54 AM
    // 1/1/1, 3:30:17 AM
    // 1/1/1, 3:27:16 AM

    console.log(date.toString());
    // output **on my machine**, should **not** be the same on yours
    // Mon Jan 01 0001 01:09:21 GMT+0009 (Central European Standard Time)


此处有更多信息(感谢 Johan Karlsson 提供的链接):

https://bugs.chromium.org/p/chromium/issues/detail?id=849404

我认为此链接中最相关的评论是:

This is working as intended and working per spec. The spec says that we have to follow the IANA timezone database.

In 1880, there's no standard timezone and America/Los_Angeles timezone offset was based on its longitude. The same is true of other timezones.

Also note that there are many timezone around the world the zone offset (and whether or not to have DST or when to start DST) have changed multiple times even since 2000 (e.g. Europe/Moscow). The change to make them work correctly also brought in what's reported here.

关于javascript - iso '' 0001-01-01T01 :00:00' adding 16 second 创建日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461643/

相关文章:

javascript - jQuery slideToggle 嵌套 div

javascript - CSS 无法与 NodeJS 一起使用

javascript - Spotify 获取艺术家信息 - 错误 503 服务

vb.net - 使用自定义格式化程序的 Date.Parse

Java - Calendar.set(HOUR_OF_DAY) 出现意外结果

python - 在python中获取特定时间和时区的偏移量

javascript - 需要使用 Google Apps 脚本列出 google 域下的所有网站

php - 替代 MySQL 中的 strtotime()

datetime - 如何从 Neo4j 中的数据库中获取日期时间

java - 如何在 Java 中为时间戳添加/减去时区偏移量?