gpt4 book ai didi

javascript - Firefox new Date() 从字符串构造本地时区时间

转载 作者:可可西里 更新时间:2023-11-01 02:58:18 27 4
gpt4 key购买 nike

我正在尝试从字符串创建日期对象。我以 ISO 格式获取日期,除了毫秒部分,如“2012-01-30T16:23:12”

当我在 IE、Chrome 和 Firefox 中运行以下代码时,结果不同( Link to Fiddle )

currentDate = "2012-01-30T16:23:12";
var date = new Date(currentDate);
alert(date);

IE 和 Chrome 将字符串视为 UTC,但 firefox 将其视为本地时区。

除了到处检查用户代理之外,是否有任何通用的方法来绕过它?

最佳答案

您可以尝试为 UTC 附加零时区偏移 +00:00:

currentDate = "2012-01-30T16:23:12+00:00";

这有帮助吗? (抱歉,如果不实际更改我的时区,我无法对其进行测试。)

关于javascript - Firefox new Date() 从字符串构造本地时区时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062863/

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