gpt4 book ai didi

javascript - toISOString 在 Firefox 中不起作用

转载 作者:行者123 更新时间:2023-11-30 12:03:23 25 4
gpt4 key购买 nike

我正在为 ISOString 创建一个新日期 -

new Date(03-13-2016 00:00).toISOString();

这在 IE 和 Chrome 中工作正常,但在 FireFox 中不行。

我试过稍微像修改字符串一样-

new Date(03-13-2016T00:00:00Z).toISOString();

然而这也失败了。我怎样才能获得预期的结果以在所有浏览器中工作?

2016-03-13T00:00:00.000Z

PS 我知道我从一个字符串开始,然后尝试使用 toISOString 创建一个字符串 - 原因是这需要在一行中处理与 UTC 的时区偏移。

最佳答案

当您将字符串传递给 Date 构造函数时,它会在内部调用 Date.parse 以尝试从中获取有效日期。这首先检查它是否是 the Date Time formats 之一在规范中。如果不是("03-13-2016 00:00""03-13-2016T00:00:00Z" 都不是),parse specification继续说:

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

在这种情况下,似乎 IE 和 Chrome 都有代码可以正确解析它,而 Firefox 没有。你真正要解决这个问题的唯一方法是拥有一个符合规范的字符串,或者用 individual date/time component parts 调用构造函数.

关于javascript - toISOString 在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015877/

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