2

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.setDate( 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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was answering at the same time as you and didn't see your answer. I'm not gonna steal your reputation points, don't worry.
When someone answers while I answer, I see a big fat orange banner across the screen. If I click it, it loads the answers that has been posted while I wrote. So when I saw the same answer but 5 minutes later, I did wonder. немає проблем
1

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)} 

3 Comments

I understand what you're saying - that's what I've done in my selfref tests at the bottom. That makes complete sense to me. What I find odd is in the first set - I'm doing a setDate on the test variable with the original (and unchanging) date variable. The outcome of "date.getDate() + 1" should always be the next day - yet unless I reset test between runs, it jumps by a month. Why is the outcome of test.setDate( date.getDate() + 1 ) different based on how many time I've run the exact same command? Shouldn't date.getDate() be invariant and unrelated to any internal state of test?
Oh, I see what you are saying - it is affected by the internal state of the original test variable. I hadn't quite realized that setDate was literally just setting the day of the month of the object, I thought it was more of a holistic view of the date (which would include month and year). I get now that setting date to (32) changes the month and setting it again jumps the month forward again. Thanks!
@Felix, when you did not change the object named date that you initialised in the very beginning, you got 32 each time added to whatever you had set the test date object to before.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.