Safari and Javascript Date format

Update: Apple replied to my bug report that this issue is fixed in the latest version of Safari though you may still want to use this workaround to be compatible with older version of Safari.

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


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