Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari returns incorrect value for Date toISOString()

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).

However, what I am seeing is:

Safari (incorrect):

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

Chrome (correct):

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

FireFox (correct):

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

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

like image 360
John Doherty Avatar asked Feb 16 '19 18:02

John Doherty


Video Answer


1 Answers

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 (correct):

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

Chrome (correct):

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

FireFox (correct):

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

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

like image 182
John Doherty Avatar answered Sep 20 '22 16:09

John Doherty