Skip to content Skip to sidebar Skip to footer

Javascript Setdate() Unexpected Behaviour Wrong Month

Can anyone explain to me why the month is set incorrectly in 'defaultDate.setDate(d.getDate());' in the code below? I know that JS runs months from a 0 index, however I would have

Solution 1:

setDate() and getDate() functions only refer to the day of the month. When you subtract 10 days from the d object it automatically sets the month too, so as July. But, when you set the date of defaultDate object, you only set the days(so the Month and day of week is not changed).

Solution 2:

setDate() expects number from 1-31 range, so when you pass it -1 (today is 9th, so 9-10 = -1) you can't expect it to do anything meaningful. What would you do, if I told you to set your calendar to -1st of August?

Post a Comment for "Javascript Setdate() Unexpected Behaviour Wrong Month"