Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date parsing in javascript is different between safari and chrome

I have the following code

var c = new Date(Date.parse("2011-06-21T14:27:28.593Z")); console.log(c); 

On Chrome it correctly prints out the date on the console. In Safari it fails. Who is correct and more importantly what is the best way to handle this?

like image 820
bradgonesurfing Avatar asked Jun 21 '11 14:06

bradgonesurfing


People also ask

What is the use of date parse in Javascript?

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

How do I change the date format in Safari?

To change the date format on a Mac, click the Apple icon → Click "System Preferences" → Click "Language & Region" → Click "Advanced" → Click "Dates"→ Customize your format.


1 Answers

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); 
like image 190
Erik Avatar answered Sep 20 '22 03:09

Erik