3

The following code:

//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var today = new Date(2013,0,31); 
var tomorrow = new Date(); 
tomorrow.setDate(today.getDate() + 1);
alert("New date is "+tomorrow.getFullYear() +", "+ tomorrow.getMonth()+", "+ tomorrow.getDate())

...outputs: 2014, 1, 1

(Demo: http://jsfiddle.net/3pA3Q/5/)

Can anyone explain this?

Also, these two have the same result:

var today = new Date(2013,11,31); 
var today = new Date(2013,12,31); 

I understand "month beginning with 0 for January to 11 for December", so new Date(2013,12,31) should be Year 2014, January, 31st

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
James King
  • 1,574
  • 4
  • 19
  • 28
  • Please include code directly in your questions, don't rely on links to external sites (I've edited it in for you). The two lines that you say have the "same result" create date objects for two different dates, but then your other code uses `today.getDate()` which retrieves only the day of the month part of the date, which _will_ be the same... – nnnnnn Jan 17 '14 at 00:07
  • @nnnnnn Yeah I double tested. It is the same "2014, 1, 1". – James King Jan 17 '14 at 00:10
  • The `tomorrow` date ends up the same either way because you are only using the day of the month from the `today` date. – nnnnnn Jan 17 '14 at 00:11
  • @nnnnnn I know that. But http://jsfiddle.net/3pA3Q/10/ is even stranger according to http://stackoverflow.com/questions/3674539/javascript-date-increment-question – James King Jan 17 '14 at 00:13
  • _"Also, these two have the same result:"_ I'm not sure what you mean by that but they don't result in the same value. One is Dec 31 2013 and the other Jan 31 2014. http://jsfiddle.net/j08691/rCmwe/ – j08691 Jan 17 '14 at 00:13
  • @JamesKing that question does `.setTime()` your do `.setDate()` but pass it a `getTime()` as parameter.. you have messed your values a bit.. – Gabriele Petrioli Jan 17 '14 at 00:15
  • James that newer fiddle is using `.setDate()` to set the day of the month to the number `1358773200000`. In theory if you set a day higher than fits in the current month then the month and possibly year get updated automatically, but for `1358773200000` that ends up billions of years in the future so I'm guess JS can't cope and you end up with an invalid date. Hence `NaN` when you try to retrieve the parts of the invalid date. – nnnnnn Jan 17 '14 at 00:15
  • @GabyakaG.Petrioli Thanks Get it now. – James King Jan 17 '14 at 00:17
  • @j08691 Oh I mean if you put these two lines in the jsfiddle code (replkacing the original `var today=` line), the final result is the same. Anyway, I get everything now. Thanks you guys. – James King Jan 17 '14 at 00:22

1 Answers1

3

You initialised tomorrow to be todays date, so in this line tomorrow.setDate(today.getDate() + 1); you're simply adding 1 day to todays date.

You would be better off cloning your date:

var today = new Date(2013,0,31); 
var tomorrow = new Date(today.getTime()); // Get a copy
tomorrow.setDate(tomorrow.getDate() + 1);
itsmejodie
  • 4,148
  • 1
  • 18
  • 20