Skip to content
Advertisement

Safari Javascript Date() NaN Issue (yyyy-MM-dd HH:mm:ss)

My code is working properly in Google Chrome, but not in Safari.

I figured out that I need to convert yyyy-MM-dd HH:mm:ss to ISO 8601, but I didn’t found a solution to do this.


Online Test Link: http://jsfiddle.net/UVgHR/


Javascript:

JavaScript

HTML Example:

JavaScript

Advertisement

Answer

To make your question easier your problem is with:

JavaScript

This work okay in chrome but not in safari. The mdn talks about ECMAScript 5 ISO-8601 format support says:

Alternatively, the date/time string may be in ISO 8601 format. For example, “2011-10-10” (just date) or “2011-10-10T14:48:00” (date and time) can be passed and parsed.

If you include T it works:

JavaScript

You can use new Date('2014-02-18T15:00:48'.replace(/s/, 'T')).

If you handle a lot of cases like this I will recommend using moment which seems to handle this case very well with or without T: parsing from string. Additionally your whole example is easier with momentjs:

JavaScript
Advertisement