5

My eyes must be deceiving me, but when I call setMonth(1) shouldn't the date of that date object be February? (Today is March 30, 2016)

x = new Date();
y = new Date();
x.setMonth(1);

document.write(x.toString() + '<br>' + y.toString());

Results in:

Naftali
  • 144,921
  • 39
  • 244
  • 303
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
  • Adam -- this code will only fail today (of all days) Instead can you please update your post to say that today is March 30, 2016 and then take a screenshot of the results. – Naftali Mar 30 '16 at 14:16
  • If you mouse over the fuzzy dates, you can see the exact time the post was made. – alex Mar 30 '16 at 14:40
  • These two topics about javascript date validation might be helpful: http://stackoverflow.com/questions/6177975/how-to-validate-date-with-format-mm-dd-yyyy-in-javascript and http://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript?lq=1 – Kağan Kayal Mar 30 '16 at 15:15

1 Answers1

10

There is no February 30, 2016

So therefore the date processor correctly changes the date to March 1, 2016

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • Just looking at [the spec for this](http://www.ecma-international.org/ecma-262/6.0/#sec-makeday) _"7. Find a value t such that..."_ - that's rather vague :) – James Thorpe Mar 30 '16 at 14:24
  • Hmmm? What do you mean @JamesThorpe ? – Naftali Mar 30 '16 at 14:25
  • Doesn't specify how to go about finding that value - it's usually quite specific about algorithms like this - just leaves it completely up to the implementation. Just seemed out of place, especially when the next step is then very specific: _"Return Day(t) + dt − 1"_. – James Thorpe Mar 30 '16 at 14:26