4
votes

I am passing this new Date into both Firefox and Chrome console (same computer, and time zone) and I am getting mixed results.So confusing... In chrome new Date(); //Wed Dec 09 2015 18:06:55 GMT+0530 (IST)

In firefox new Date(); //Date 2015-12-09T12:36:34.410Z

3

3 Answers

8
votes

Your confusion is caused by different time-zones displaying.

Your Chrome gives you the time in UTC+0, while Firefox gives you the time in GMT+0530.

You can specify you want both to always be UTC by writing

var myDate = new Date();
myDate.toISOString() // will give you a date in the format you see by Chrome
0
votes

What you are seeing is the result of Date.prototype.toString, which is entirely implementation dependent. So you may well see a different string in every client you test.

You can use toISOString to get an ISO 8601 format string that is UTC. There is a polyfill on MDN.

document.write(new Date().toISOString());
0
votes

Firefox does not like the '-' in the string

Replace all occurrences of - with / using a regular expression and then convert the string to Date object.

var str = '01-25-2019 10:28:15 AM';

str = str.replace(/-/g,'/');  //replaces all occurances of "-" 
with "/"

var dateobject = new Date(date_string);

alert(dateobject.toDateString());