Invalid date in safari Invalid date in safari javascript javascript

Invalid date in safari


For me implementing a new library just because Safari cannot do it correctly is too much and a regex is overkill.Here is the oneliner:

console.log (new Date('2011-04-12'.replace(/-/g, "/")));


The pattern yyyy-MM-dd isn't an officially supported format for Date constructor. Firefox seems to support it, but don't count on other browsers doing the same.

Here are some supported strings:

  • MM-dd-yyyy
  • yyyy/MM/dd
  • MM/dd/yyyy
  • MMMM dd, yyyy
  • MMM dd, yyyy

DateJS seems like a good library for parsing non standard date formats.

Edit: just checked ECMA-262 standard. Quoting from section 15.9.1.15:

Date Time String Format

ECMAScript defines a stringinterchange format for date-timesbased upon a simplification of the ISO8601 Extended Format. The format isas follows: YYYY-MM-DDTHH:mm:ss.sssZWhere the fields are as follows:

  • YYYY is the decimal digits of the year in the Gregorian calendar.
  • "-" (hyphon) appears literally twice in the string.
  • MM is the month of the year from 01 (January) to 12 (December).
  • DD is the day of the month from 01 to 31.
  • "T" appears literally in the string, to indicate the beginning ofthe time element.
  • HH is the number of complete hours that have passed since midnight as twodecimal digits.
  • ":" (colon) appears literally twice in the string.
  • mm is the number of complete minutes since the start of the hour astwo decimal digits.
  • ss is the number of complete seconds since the start of the minuteas two decimal digits.
  • "." (dot) appears literally in the string.
  • sss is the number of complete milliseconds since the start of thesecond as three decimal digits. Boththe "." and the milliseconds field maybe omitted.
  • Z is the time zone offset specified as "Z" (for UTC) or either "+" or "-"followed by a time expression hh:mm

This format includes date-only forms:

  • YYYY
  • YYYY-MM
  • YYYY-MM-DD

It also includes time-only forms withan optional time zone offset appended:

  • THH:mm
  • THH:mm:ss
  • THH:mm:ss.sss

Also included are "date-times" whichmay be any combination of the above.

So, it seems that YYYY-MM-DD is included in the standard, but for some reason, Safari doesn't support it.

Update: after looking at datejs documentation, using it, your problem should be solved using code like this:

var myDate1 = Date.parseExact("29-11-2010", "dd-MM-yyyy");var myDate2 = Date.parseExact("11-29-2010", "MM-dd-yyyy");var myDate3 = Date.parseExact("2010-11-29", "yyyy-MM-dd");var myDate4 = Date.parseExact("2010-29-11", "yyyy-dd-MM");


I was facing a similar issue. Date.Parse("DATESTRING") was working on Chrome (Version 59.0.3071.115 ) but not of Safari (Version 10.1.1 (11603.2.5) )

Safari:

Date.parse("2017-01-22 11:57:00")NaN

Chrome:

Date.parse("2017-01-22 11:57:00")1485115020000

The solution that worked for me was replacing the space in the dateString with "T". ( example : dateString.replace(/ /g,"T") )

Safari:

Date.parse("2017-01-22T11:57:00")1485086220000

Chrome:

Date.parse("2017-01-22T11:57:00")1485115020000

Note that the response from Safari browser is 8hrs (28800000ms) less than the response seen in Chrome browser because Safari returned the response in local TZ (which is 8hrs behind UTC)

To get both the times in same TZ

Safari:

Date.parse("2017-01-22T11:57:00Z")1485086220000

Chrome:

Date.parse("2017-01-22T11:57:00Z")1485086220000