Firefox new Date () from string builds time in local timezone

I am trying to create a date object from a string. I get the date in ISO format, except for the millisecond part, such as "2012-01-30T16: 23: 12"

The results are different when I run the following code in IE, Chrome and Firefox ( Script Link )

currentDate = "2012-01-30T16:23:12"; var date = new Date(currentDate); alert(date); 

IE and Chrome treat the string as UTC, but firefox treats it in the local time zone.

Is there any general way to get around it except to check the user agent everywhere?

+9
javascript date timezone
Jan 30 2018-12-12T00:
source share
3 answers

You can try adding a zero time zone offset +00:00 for UTC:

 currentDate = "2012-01-30T16:23:12+00:00"; 

Does it help? (Sorry, I cannot verify this without changing my time zone.)

+12
Jan 30 '12 at 11:21
source share

Hm, a possible workaround is to analyze strings and usage methods.

 setUTCDate() setUTCFullYear() setUTCHours() 

Perhaps there is a better solution

+3
Jan 30 '12 at 11:15
source share

There is no guarantee that the input will be properly parsed if in this format. The Date.parse () procedure is required only for parsing strings in a specific format - parsing of other formats is implementation dependent. If you dare to rely on implementations that satisfy the requirement, add the data to the specific format:

 new Date(currentDate + '.000Z') 

Alternatively, use a library that can analyze data in the current format, for example. jQuery or Globalize.js.

Similar considerations apply to the date of recording. There is no guarantee of the output format if you use Date.toString() , explicitly or as in alert(date) . Even on the same computer, different browsers will use different localized formats.

0
Jan 30 '12 at 11:44
source share



All Articles