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?
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).
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With