Incorrect Javascript Date in Chrome vs Firefox

I am getting the wrong dates in Chrome ...

My code looks like this.

The header contains "2013-06-14T00: 00: 00", it was a DateTime in C # returned from the WebAPI

As you can see here in both browsers .. enter image description here

When I add it to a new javascript date like this. var dt = new Date(title)

I get different dates in different browsers ... enter image description here

Example - http://jsfiddle.net/RvUSq/

+4
source share
2 answers

Convert timestamp to ISO 8601 formatted string in C # e.g.

 var title = "14 JUN 2013 00:00:00" // printed from C# 

Then use the Date constructor

 var date = new Date(title); 

If you do not specify a time zone, the local time zone on the client machine will be set to the specified time. If you specify a time zone, the necessary calculations will be made to convert the date to a local time zone.

 var title = "14 JUN 2013 00:00:00"; var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST) var title = "14 JUN 2013 00:00:00 GMT"; var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST) var title = "14 JUN 2013 00:00:00 GMT-0400"; var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST) 

ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

+3
source

Firefox seems to suggest that this date and time format without a time zone is local time, and Chrome / Webkit accepts this UTC.

If the datetime returned from the api is UTC, just add ā€œZā€ to the end of the line, so it will become ā€œ2013-06-14T00: 00: 00Zā€, which indicates the time in UTC, then you will get the same result in two browsers.

+14
source

Source: https://habr.com/ru/post/1488410/


All Articles