Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox new Date() from string constructs time in local time zone

I am trying to create a date object from a string. I get date in ISO format except milliseconds part like "2012-01-30T16:23:12"

Results differ when I run following code in IE, Chrome and Firefox (Link to Fiddle)

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

IE and Chrome considers the string as UTC but firefox considers in local time zone.

Is there any generic way to get around it except for checking user agent everywhere?

like image 453
harsha Avatar asked Jan 30 '12 11:01

harsha


2 Answers

You could try appending the zero timezone offset +00:00 for UTC:

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

Does that help? (Sorry I can't test it without actually changing my timezone.)

like image 199
Rob Agar Avatar answered Sep 22 '22 04:09

Rob Agar


Hm, possible workaround is to parse string and use methods.

setUTCDate()    
setUTCFullYear()
setUTCHours()

Probably, there is better solution

like image 40
Anton Avatar answered Sep 20 '22 04:09

Anton