Mon Jan 01 0001 02:50:16 GMT+0150 (Moscow Standard Time) GMT 不正-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:09:07 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com