Safari为ISOString()返回Date的错误值 [英] Safari returns incorrect value for Date toISOString()

查看:303
本文介绍了Safari为ISOString()返回Date的错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将日期字符串2019-02-16T10:00:00转换为时区GMT+0100 (CET)中的JS Date对象,然后调用.toISOString()时,我希望获得ISO日期/时间2019-12-01T09:10:10.000Z (-1小时).

When I convert the date string 2019-02-16T10:00:00 into a JS Date object in timezone GMT+0100 (CET), and then call .toISOString() I expect to get the ISO date/time 2019-12-01T09:10:10.000Z (-1 hour).

但是,我看到的是:

Safari (错误):

Safari (incorrect):

new Date('2019-12-01T10:10:10').toISOString()
\\ returns 2019-12-01T10:10:10.000Z

Chrome (正确):

Chrome (correct):

new Date('2019-12-01T10:10:10').toISOString()
\\ returns 2019-12-01T09:10:10.000Z

FireFox (正确):

new Date('2019-12-01T10:10:10').toISOString()
\\ returns 2019-12-01T09:10:10.000Z

我是否缺少某些东西,或者这是一个已知的Safari问题?

Am I missing something, or is this a known Safari issue?

推荐答案

我发现了问题. Safari无法将格式为2019-12-01T10:10:10的日期字符串转换为Date对象,而无需对其进行任何操作.解决方案(在此处找到)是将其重新格式化为所有浏览器都支持的2019/12/01 10:10:10.

I found the problem. Safari is unable to convert a date string in the format 2019-12-01T10:10:10 into a Date object without screwing with it. The solution (found here) is to reformat to 2019/12/01 10:10:10 which is supported by all browsers.

// convert into YYYY/MM/DD HH:MM:SS
var dateString = '2019-12-01T10:10:10'.replace(/-/g, '/').replace('T', ' ');

Safari (正确):

Safari (correct):

new Date(dateString).toISOString()
\\ returns 2019-12-01T09:10:10.000Z

Chrome (正确):

new Date(dateString).toISOString()
\\ returns 2019-12-01T09:10:10.000Z

FireFox (正确):

new Date(dateString).toISOString()
\\ returns 2019-12-01T09:10:10.000Z

希望这可以节省下一个沮丧的开发人员几个小时!

Hope this saves the next frustrated developer a couple of hours!

这篇关于Safari为ISOString()返回Date的错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆