Date Parsing in JavaScript Is Different Between Safari and 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);

javascript date.parse difference in chrome and other browsers

Here is a fix for Firefox and IE/Safari (with the help from JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse
) :

DEMO

var noOffset = function(s) {
var day= s.slice(0,-5).split(/\D/).map(function(itm){
return parseInt(itm, 10) || 0;
});
day[1]-= 1;
day= new Date(Date.UTC.apply(Date, day));
var offsetString = s.slice(-5)
var offset = parseInt(offsetString,10)/100;
if (offsetString.slice(0,1)=="+") offset*=-1;
day.setHours(day.getHours()+offset);
return day.getTime();
}

From MDN

JavaScript 1.8.5 note

A subset of ISO 8601 formatted date strings can now also be parsed.

Alternatively, the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed. Timezones in ISO dates are not yet supported, so e.g. "2011-10-10T14:48:00+0200" (with timezone) does not give the intended result yet.

javascript date variable formatting - chrome vs safari

Hmm

new Date('2012-06-07 13:47:08'); // works

therefore your given parameter must actually have type date. Because the console prints the date. If it's a date then you are trying to do something like that:

var date = new Date();
new Date(date); // doesn't work

And that doesn't work.

To check if your variable is of type date try this:

var isDate = function(date) {
return Object.prototype.toString.call(date) === "[object Date]";
};

Ok, found solution:
http://jsfiddle.net/nPSqL/2/

Got it from new Date() using Javascript in Safari

Javascript String to Date works in Chrome, not FF or Safari

The full ISO8601 format (IIRC) is expecting a capital letter "T" in between the date and time portions of that string, e.g. ""2014-11-22T00:00:00.0"" https://en.wikipedia.org/wiki/ISO_8601 (I'd therefore recommend always adding the T delimiter.

E.g. the following code will parse to NaN in some browsers:

var myAlmostISODate = "2014-11-22 00:00:00.0";
console.log("Date: " + Date.parse(myAlmostISODate));
//IE11: NaN
//Firefox: 1416632400000
//Chrome: 1416632400000


Related Topics



Leave a reply



Submit