Javascript and Safari Date format


Update: Apple replied to my bug report about Javascript and the Safari Date format saying that this issue is fixed in the latest version of Safari. However, you may still want to use this workaround to be compatible with older versions 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.  With a date in 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