4

[Please help me improve the title if it doesn't describe the question clear]


I found this weird thing when I develop a jquery plugin:

var a = new Date('1986-05-03');
a.setHours(0,0,0,0);
// a = Sat May 03 1986 00:00:00 GMT+0800 (CST)

a.setDate( a.getDate() + 1 );
// a = Sat May 03 1986 23:00:00 GMT+0800 (CST)

It actually adds 23 hours

There's some other ticket mentioned daylight savings, but May 3 is not the beginning of daylight saving days, right ?

Further, I tried to print all the not-24-hours date, here's my code:

var start = new Date('1900-01-01'); // FYI, IE8- doesn't support this kind of date construction
var end = new Date('2014-01-01');
start.setHours(0,0,0,0);
end.setHours(0,0,0,0);

while (start.getTime() < end.getTime()) {
    var oneMoreDay = new Date(start.getTime()); 
    oneMoreDay.setDate(start.getDate() + 1);
    var diff = oneMoreDay.getTime() - start.getTime();
    if (diff != 86400000) {
        console.log(start);
    }
    start = oneMoreDay;
} 

Here's the output:

Sat May 03 1986 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 12 1986 23:00:00 GMT+0900 (CST) VM124:12
Sat Apr 11 1987 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 11 1987 23:00:00 GMT+0900 (CST) VM124:12
Sat Apr 09 1988 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 09 1988 23:00:00 GMT+0900 (CST) VM124:12
Sat Apr 15 1989 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 15 1989 23:00:00 GMT+0900 (CST) VM124:12
Sat Apr 14 1990 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 14 1990 23:00:00 GMT+0900 (CST) VM124:12
Sat Apr 13 1991 23:00:00 GMT+0800 (CST) VM124:12
Fri Sep 13 1991 23:00:00 GMT+0900 (CST) 

So, it's not in every year! Why are these date special?

Phoenix
  • 756
  • 1
  • 7
  • 22

2 Answers2

7

It is every April/May and September and it gives you a different GMT offset. That is daylight savings.

From Wikipedia:

After the Chinese Civil War, in 1949, a unified time zone—GMT+8—was established by the People's Republic of China for all its territories, called Beijing Time (sometimes known as Chinese Standard Time). Daylight saving time was observed from 1986 to 1991.

It isn't a problem. The time is correct. The time offset just changes. Ignore it, all calculation will work correctly.

P.S. That is the first time I had to research the Chinese Civil War for stack overflow.

Damien Black
  • 5,579
  • 18
  • 24
  • Is that mean I have this problem because my local time is Chinese time? – Phoenix Jan 21 '14 at 04:09
  • It isn't a problem. The time is correct. The time offset just changes. Ignore it, all calculation will work correctly. – Damien Black Jan 21 '14 at 04:10
  • +1 for Chinese civil war research :) Not the first [odd Asian time-zone questions I've seen around here](http://stackoverflow.com/a/6841479/1348195). – Benjamin Gruenbaum Jan 21 '14 at 04:10
  • Thank you so much ! I'm working on a timeline widget so I need the time increase by exactly 24 hours. I'm using `a.setTime(a.getTime()+86400000);` now and it works well. – Phoenix Jan 21 '14 at 04:14
0

It is two days (beginning and end) for every year that using a daylight-saving calendar in your local timezone.

Daylight saving time was observed only from 1986 to 1991 in China, so I ran the code in Chinese time zone I got the above results.

But if I change my timezone to London time, I get the different output, it's two days in almost every year, March and October.

You can run that code in different timezone to see the differences.

Phoenix
  • 756
  • 1
  • 7
  • 22