Date parsing in javascript is different between safari and chrome Date parsing in javascript is different between safari and chrome google-chrome google-chrome

Date parsing in javascript is different between safari and chrome


You can't really use Date.parse. I suggest you use: new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )

To split the string you could try

var s = '2011-06-21T14:27:28.593Z';var a = s.split(/[^0-9]/);//for (i=0;i<a.length;i++) { alert(a[i]); }var d=new Date (a[0],a[1]-1,a[2],a[3],a[4],a[5] );alert(s+ " "+d);


My similar issue was caused by Safari not knowing how to read the timezone in a RFC 822 time zone format. I was able to fix this by using the ISO 8601 format. If you have control of the date format I got this working with java's SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ss.sssXXX" which produces for me ie. "2018-02-06T20:00:00.000+04:00". For whatever reason Safari can't read "2018-02-06T20:00:00.000+0400", notice the lack of colon in the timezone format.

// Worksvar c = new Date("2018-02-06T20:00:00.000+04:00"));console.log(c);// Doesn't workvar c = new Date("2018-02-06T20:00:00.000+0400"));console.log(c);


I tend to avoid Date.parse, as per the other answers for this question. It doesn't seem to be a portable way to reliably deal with dates.

Instead, I have used something like the function below. This uses jQuery to map the string array into a number array, but that's a pretty easy dependency to remove / change. I also include what I consider sensible defaults, to allow you to parse 2007-01-09 and 2007-01-09T09:42:00 using the same function.

function dateFromString(str) {  var a = $.map(str.split(/[^0-9]/), function(s) { return parseInt(s, 10) });  return new Date(a[0], a[1]-1 || 0, a[2] || 1, a[3] || 0, a[4] || 0, a[5] || 0, a[6] || 0);}