Javascript setHours (1) does not work during March 27, 2011 01:00:00 GMT + 0100

I am completely confused, why does this not work?

I repeat the date range and just add 1 hour step by step. This went on until this week. Basically, the date falls on March 27, 2011 01:00:00 GMT + 0100. Then it just holds on and does not add anything. If I add + 3h, then it will work again, but not with +1.

I use Firebug on Firefox and also tried it in the console.

Sun Mar 27 2011 01:00:00 GMT+0100

>>> this.setHours(0);
1301180400000
>>> this.setHours(1);
1301184000000
>>> this.setHours(2);
1301184000000
>>> this.setHours(3);
1301187600000

This is the code:

Date.prototype.addHours = function (h) {
    this.setHours(this.getHours() + h);
    return this;
}

I have the same error in Safari and Chrome.

+3
source share
4 answers

Daylight saving time causing this behavior. March 27 - the day of the beginning of the DST.

Edit:

Hope this solves your problem: Daylight Saving Time in JavaScript

+5
source

: ?

+2

, , , - , UTC, :

Date.prototype.addHours = function (h) {
    this.setUTCHours(this.getUTCHours() + h);
    return this;
}
+2

I had the same problem as yours and resolved it with

Date.prototype.addHours = function (h) {
    this = new Date(this.getTime() + h*3600000);
    return this;
}

I'm not sure if creating a new Date object is a good idea, but it works for me.

+1
source

All Articles