Skip to content Skip to sidebar Skip to footer

Chrome Does Not Return Correct Hour In Javascript

First try is in IE 9 console: new Date('2013-10-24T07:32:53') Thu Oct 24 07:32:53 UTC+0200 2013 returns as expected Next try is in FireFox 24 console: new Date('2013-10-24T07:32

Solution 1:

Its no bug. The implementation of date parse function differs across browsers & so does the format of the dateString accepted by it.

However this format seems to work same across ... link:

newDate("October 13, 1975 11:13:00")

If possible, try and use

newDate(year, month, day, hours, minutes, seconds, milliseconds)

for guaranteed results.


Regarding your format try parsing it yourself. Something like :

var str ='2013-10-24T07:32:53'.split("T");
var date= str[0].split("-");
var time= str[1].split(":");

var myDate =newDate(date[0], date[1]-1, date[2], time[0], time[1], time[2], 0);

Note (Thanks to RobG for this) : The Date constructor used above expects month as 0 - 11 & since October is 10 as per date String, the month has to be modified before passing it to the constructor.

Reference.

Solution 2:

See this thread:

Why does Date.parse give incorrect results?

It looks like the behavior of the parsing signature of the Date constructor is completely implementation dependent.

Solution 3:

Given:

var s = '2013-10-24T07:32:53';

in ES5 compliant browsers you could do:

var d = newDate(s + 'Z');

but for compatibility across all browsers in use, better to use (assuming date is UTC):

functiondateFromString(s) {
  s = s.split(/\D/);
  returnnewDate(Date.UTC(s[0],--s[1],s[2],s[3],s[4],s[5]));
}

Post a Comment for "Chrome Does Not Return Correct Hour In Javascript"