86

I am trying to create a new date in javascript.

I have year, month and day. Following this tutorial, syntax for creating new date should be:

new Date(year, month, day, hours, minutes, seconds, milliseconds)

and that is exactly what I am doing:

var d = new Date(2016, 12, 17, 0, 0, 0, 0);

This should be december 17th 2016, but in my console output I see:

Tue Jan 17 2017 00:00:00 GMT+0100 (Central Europe Standard Time)

What am I doing wrong?

FrenkyB
  • 6,625
  • 14
  • 67
  • 114
  • 8
    Months start from 0. You should use this: var d = new Date(2016, 11, 17); – Antonio Dec 05 '16 at 19:40
  • 2
    In Javascript Date Object, months are 0 based. So 0 means January, 11 means December. try `var d = new Date(2016, 11, 17, 0, 0, 0, 0);` It should be fine. – Hiren Dec 05 '16 at 19:42

2 Answers2

103

January is month 0. December is month 11. So this should work:

var d = new Date(2016, 11, 17, 0, 0, 0, 0);

Also, you can just simply do:

var d = new Date(2016, 11, 17);
Drew13
  • 1,301
  • 5
  • 28
  • 50
Dean Coakley
  • 1,675
  • 2
  • 11
  • 25
66

According to MDN - Date:

month

Integer value representing the month, beginning with 0 for January to 11 for December.

You should subtract 1 from your month:

const d = new Date(2016, 11, 17, 0, 0, 0, 0);
Elias Soares
  • 9,884
  • 4
  • 29
  • 59