开发者

Javascript Date object odd behaviour/bug?

开发者 https://www.devze.com 2023-03-09 07:07 出处:网络
I\'m working with javascript Date objects and seeing very odd behavior. If I keep setting a date object to another date object + an offset, it starts adding 30 days or so. If I reset the date object i

I'm working with javascript Date objects and seeing very odd behavior. If I keep setting a date object to another date object + an offset, it starts adding 30 days or so. If I reset the date object in between attempts or if I do the date math on the same object it works as expected.

Probably easier to see the test bed I'm running in the latest chrome/ff:

var date = new Date();
console.log("start:", date);

var test = new Date();
test.setDate( date.getDate() + 1 );
console.log("test1:", date, test);
test.setDate( date.getDate() + 1 );
console.log("test2:", date, test);
test.se开发者_如何学GotDate( date.getDate() + 1 );
console.log("test3:", date, test);

test = new Date();
test.setDate( date.getDate() + 1 );
console.log("test reset 1:", date, test);
test = new Date();
test.setDate( date.getDate() + 1 );
console.log("test reset 2:", date, test);
test = new Date();
test.setDate( date.getDate() + 2 );
console.log("test reset 3:", date, test);

test = new Date();
test.setDate( test.getDate() + 1 );
console.log("test selfref 1:", test);
test.setDate( test.getDate() + 1 );
console.log("test selfref 2:", test);

And the output I get:

"start:" Tue May 31 2011 13:09:13 GMT-0400 (EDT)
"test1:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Wed Jun 01 2011 13:09:13 GMT-0400 (EDT)
"test2:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Sat Jul 02 2011 13:09:13 GMT-0400 (EDT)
"test3:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Mon Aug 01 2011 13:09:13 GMT-0400 (EDT)
"test reset 1:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Wed Jun 01 2011 13:09:13 GMT-0400 (EDT)
"test reset 2:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Wed Jun 01 2011 13:09:13 GMT-0400 (EDT)
"test reset 3:" Tue May 31 2011 13:09:13 GMT-0400 (EDT) Thu Jun 02 2011 13:09:13 GMT-0400 (EDT)
"test selfref 1:" Wed Jun 01 2011 13:09:13 GMT-0400 (EDT)
"test selfref 2:" Thu Jun 02 2011 13:09:13 GMT-0400 (EDT)

So you can see that test1-3 the original date object remains the same, but the test object which should always be the original date + 1 day - goes all over the place.

Subsequently the reset tests and the selfref tests seem to work as expected.

Is this a bug or am I misunderstanding something?

felix


You are setting a day of month with the call. Since today is 31-st, you're setting the date to 32-nd which in turn gives you the next month (by subtracting the number of days in it). The call is modifying the date every time so the date is increasing. While subsequent calls to setDate(1) will not make any changes, calls to setDate(32) are "moving" the object to the next month.


You keep adding 32 days from the date.getDate()+1

If you change to

var test = new Date();
test.setDate( test.getDate() + 1 );
console.log("test1:", date, test);
test.setDate( test.getDate() + 1 );
console.log("test2:", date, test);
test.setDate( test.getDate() + 1 );
console.log("test3:", date, test);

You get

start: Date {Tue May 31 2011 19:21:34 GMT+0200 (CEST)}
test1: Date {Tue May 31 2011 19:21:34 GMT+0200 (CEST)} Date {Wed Jun 01 2011 19:21:34 GMT+0200 (CEST)}
test2: Date {Tue May 31 2011 19:21:34 GMT+0200 (CEST)} Date {Thu Jun 02 2011 19:21:34 GMT+0200 (CEST)}
test3: Date {Tue May 31 2011 19:21:34 GMT+0200 (CEST)} Date {Fri Jun 03 2011 19:21:34 GMT+0200 (CEST)}


When you call "test.setDate()", you modify the date object. Thus, the value of "test" changes each time; the results are therefore not surprising.

You're setting the date (day-of-month) to 32 each time. The first time moves you forward into June. The second call effectively sets the date to "June 32", which is really "July 2" (only 30 days in June). You then set the date to July 32, which will be August 1.

0

精彩评论

暂无评论...
验证码 换一张
取 消