New Date () works differently in Chrome and Firefox

I want to convert a date string to Date using javascript, use this code:

 var date = new Date('2013-02-27T17:00:00'); alert(date); 

'2013-02-27T17:00:00' is the UTC time in the JSON object from the server.

But the result of the above code is different from Firefox and Chrome:

Firefox returns:

 Wed Feb 27 2013 17:00:00 GMT+0700 (SE Asia Standard Time) 

Return Chrome:

 Thu Feb 28 2013 00:00:00 GMT+0700 (SE Asia Standard Time) 

Different 1 day, the correct result that I expect is the result of using Chrome.

Demo Code: http://jsfiddle.net/xHtqa/2/

How can I fix this problem to get the same result from both?

+79
javascript date timezone
Feb 27 '13 at 10:36
source share
5 answers

The correct format for UTC will be 2013-02-27T17:00:00Z (Z for Zulu Time). Add Z if not to get the correct UTC datetime string.

+57
Feb 27 '13 at 10:56 on
source share
— -

Yes, unfortunately, date parsing algorithms are implementation dependent. From the specification of Date.parse (which is used by new Date ):

A string can be interpreted as local time, UTC, or time in some other time zone, depending on the contents of the string. First, the function tries to analyze the format of the string in accordance with the rules written in the format of the date time string ( 15.9.1.15 ). If String does not conform to this format, the function can return to any implementation-specific heuristic, or to implementation date formats.

To make the Date constructor not (possibly) use the local time zone, use the datetime string with time zone information, for example. "2013-02-27T17:00:00Z" . However, it is difficult to find a format that can be reliably parsed by each browser - the ISO format is not recognized by IE <8 (see JavaScript: Which browsers support parsing ISO-8601 Date String with Date.parse ). Better use a unix timestamp, i.e. milliseconds with a unix epoch , or use the regulare expression to split a string into parts, and then feed these to Date.UTC .

+25
Feb 27 '13 at 11:01
source share

I found one here. It seems that the built-in console of Firefox inspectors may have an error: If I run a new date () in the built-in inspector, it shows the date with the wrong time zone, local language GMT, but with the same command in the Firebug extension console, the specified date uses my correct time zone (GMT-3: 00).

+2
Nov 24 '15 at 12:56
source share

Try using moment.js. It goes very well and in a similar way with all browsers. comes with many formatting options. ( "date of" ). format (") instead of" New date "(" date ")

0
Jan 22 '16 at 18:08
source share

Note that FireFox did not return the same result as Chrome. It looks like the format you use in kendo.toString for the date matters.

The last console result is what I need:

enter image description here

-one
Aug 04 '16 at 22:52
source share



All Articles