2

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 assumed that setDate() would take care of any discrepencies.

<script>
var defaultDate = new Date();
 window.alert(defaultDate);

function testfunction(){
    var d=new Date();
    window.alert(d);
    d.setDate(d.getDate()-10);
    window.alert(d);
    defaultDate.setDate(d.getDate());
    window.alert(defaultDate);
    }
   testfunction();
</script>
LiamRyan
  • 1,892
  • 4
  • 32
  • 61
  • Please check the this link. http://stackoverflow.com/questions/1453043/zero-based-month-numbering – Jayesh Goyani Aug 09 '13 at 12:44
  • Thank you for the link, however as stated should this not be taken care of by the setDate() logic? I am setting the date using the Date() function and subtracting 10, then I am setting the date in defaultDate to this. Nowhere in the code am I declaring a number, therefore shouldn't this work as the same method is used for both declarations? I would expect d.setDate(d.getDate() - 10); to also show the wrong month if this was a 0 index issue – LiamRyan Aug 09 '13 at 12:46
  • Additionally, from the documentation, the raw format here should be a long representing millis – LiamRyan Aug 09 '13 at 12:53

2 Answers2

2

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

hasanoviz
  • 141
  • 10
-1

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?

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • However this date is being returned correctly in d.setDate(d.getDate()-10); It only falls over at defaultDate.setDate(d.getDate()); – LiamRyan Aug 09 '13 at 12:49
  • http://www.w3schools.com/jsref/jsref_setdate.asp shows that -1 and 32 are acceptable inputs – LiamRyan Aug 09 '13 at 12:51
  • No it doesn't. `defaultDate`'s Month is `August` (as of today), and `d`'s day is 30th. So when you do `defaultDate.setDate(d.getDate())` you get August 30th. All as expected. – Mchl Aug 09 '13 at 12:53
  • "Expected values are 1-31, but other values are allowed: 0 will result in the last day of the previous month -1 will result in the day before the last day of the previous month" - therefore my understanding is that the date should be set to 30th July which is correctly stored in d, however feeding this to defaultdate results in 30th August – LiamRyan Aug 09 '13 at 12:54
  • w3schools is not an actual reference and should not be treated as such. It documents behaviors that should be left otherwise undocumented. Yes, it works. No, you shouldn't be using it. – Mchl Aug 09 '13 at 12:56
  • And once again, `setDate()` only sets DAY of given `Date` object. So why do you find it strange, that `defaultDate`'s month is not changed? – Mchl Aug 09 '13 at 12:57
  • Ah so the actual answer is that the long millis is being correctly updated due to the -1 handler in d, however since I am setting the date only in defaultDate the computation does not alter non-date areas of the long since the out of range handler isn't being invoked (If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month. - mozilla dev) – LiamRyan Aug 09 '13 at 13:00
  • Thank you for the help, however I felt the actual answer posted was misleading and did not highlight the reason for the issue so I have accepted the clearer answer – LiamRyan Aug 09 '13 at 13:10
  • The statement "*setDate() expects number from 1-31 range*" is not consistent with [*ECMA-262*](https://262.ecma-international.org/#sec-date.prototype.setdate), which does not impose any restriction on the value passed to the function, nor is it sensible to do so. – RobG Jun 22 '22 at 10:38