-1

I'm completely lost as to why the following code gives me a year of 2021 while executing this on the day of 01/03/2019... What... What am I missing... I so need a coding buddy sitting next to me I think.. >.<

function daysTillChristmas()
{
  var today = new Date();
  var christmasDay1 = new Date(today.getFullYear() +1, 12, 25);
  var test = today.getFullYear() + 1;
  var christmasDay2 = new Date(2020, 12, 25);

  console.log(today);
  console.log("Today: " + today);
  console.log("Today Full Year: " + today.getFullYear());
  console.log("ChristmasDay1: " + christmasDay1);
  console.log(christmasDay2);
  console.log("ChristmasDay2: " + christmasDay2);
  console.log("Test: " + test);
}

My output is...

"2019-01-03T22:54:33.294Z"
Today: Thu Jan 03 2019 14:54:33 GMT-0800 (Pacific Standard Time)
Today Full Year: 2019
ChristmasDay1: Mon Jan 25 2021 00:00:00 GMT-0800 (Pacific Standard Time)
"2021-01-25T08:00:00.000Z"
ChristmasDay2: Mon Jan 25 2021 00:00:00 GMT-0800 (Pacific Standard Time)
Test: 2020

BTW... I'm learning Javascript at the moment and I'm using the following URL to test out code and get some exercises to use.

Browser: Chrome 70.0.3538.77 32-bit

https://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php#EDITOR

RobG
  • 134,457
  • 30
  • 163
  • 204
Rob K.
  • 487
  • 5
  • 15
  • There are [*many duplicates*](https://stackoverflow.com/search?q=%5Bjavascript%5Dwrong+month). – RobG Jan 03 '19 at 23:26

1 Answers1

4

See MDN:

Note: The argument monthIndex is 0-based. This means that January = 0 and December = 11.

So month 12 is the 13th month of the year, which rolls over and becomes January of the next year.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264