javascript - why new Date() function gives different output in chrome and firefox - Stack Overflow

I am passing this new Date into both Firefox and Chrome console (same puter, and time zone) and I am ge

I am passing this new Date into both Firefox and Chrome console (same puter, 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

I am passing this new Date into both Firefox and Chrome console (same puter, 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

Share Improve this question asked Dec 9, 2015 at 12:56 Tanvi ShahTanvi Shah 5072 gold badges9 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

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

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());

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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745363954a4624488.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信