Safari and Javascript Date format


Recently I was working on a web App that required some date comparisons in Javascript.  Developing with both Chrome and Firefox, everything looked fine and with a date of the form YYYY-MM-DD HH:MM:SS (for example 2017-08-21 14:00:03) everything worked fine using this line of code:

var dd = new Date(device.last_modified);

Then I ran the same App on Safari and the date operation failed, Safari’s implementation of Date() returned “Invalid date”.

Sigh.

This was the fix, that made every browser happy:

var dd = new Date(device.last_modified.replace(/-/g, “/”));

I’ve raised a bug with Apple since the use of the dash character is, in fact, an international standard and Apple really ought to bring Safari up to speed.

Leave a comment