64

using Mozilla Firefox Firebug:

var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;

Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}

Why does javascript create the date with the wrong month?

Joe Kahl
  • 811
  • 1
  • 7
  • 8
  • 6
    JavaScript months are zero indexed... *finds the dupe link*. – Matt Sep 03 '12 at 21:45
  • 1
    @Matt: give me couple of minutes to collect some rep points ;-) – zerkms Sep 03 '12 at 21:45
  • Eugh, I can't believe I can't find one. Google is failing me. – Matt Sep 03 '12 at 21:48
  • 1
    @Matt: "site:stackoverflow.com date javascript month zero indexed" ? http://stackoverflow.com/questions/7834266/why-subtract-1-from-months-in-javascript-when-converting-to-a-date-object – zerkms Sep 03 '12 at 21:51

4 Answers4

108

No, javascript's Date months start with 0, so 9 is a 10th month and it is October

Reference:

new Date(year, month [, day, hour, minute, second, millisecond]);

[...]

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

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    Is this true for the day as well? I created a Date(1982, 10, 14)... sometimes it gives me 13 Oct 1982 23:00 and sometimes it gives me 14 Oct 1982 00:00 – Shumii Sep 01 '14 at 18:59
  • 2
    I think it's worth mentioning that month is zero-based not only in JavaScript but almost everywhere. This follows the tradition of `` from C. All fields in C which are used for indexing arrays are zero-based even if the human readable form would be one-based. Whenever similar APIs were then created elsewhere, most of them follow that convention, like Java or JavaScript. – Christian Hujer Dec 31 '14 at 08:49
  • 4
    only a true intellectual would understand. Clearly I am not one, as how mildly triggered I am by this – Diego T. Yamaguchi Jul 26 '18 at 12:16
  • @DiegoT.Yamaguchi happens to everybody, so relax ;-) – zerkms Jul 26 '18 at 20:26
9

In the javascript world months begin with zero! kind of weird to me. Anyhow, 9 is NOT September, but rather 9 is October.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Joe Kahl
  • 811
  • 1
  • 7
  • 8
  • 1
    This is not weird. In the programming world, most languages provide months in 0-based integers. – Derek 朕會功夫 Dec 31 '14 at 08:51
  • 2
    You know I was going to respond with "But what about days and years? Why are they not zero based?" But then I came to the realization; in all actuality, they are 2 completely different data types, Months are proper names, strings, and days of the month along with years are just numbers, integers. So it's not really weird, it actually kinda makes sense when you think about it. – Mark Carpenter Jr Feb 02 '18 at 13:50
  • 2
    @MarkCarpenterJr no I think it was a sadistic decision to have a zero-based month. Every sane person thinks 9-11 is September 11, except the jagoff that decided to arbitrarily make it 8-11 in code. Years are already zero-based, but days are not, so the same decision should have applied to days. – AndrewBenjamin Apr 07 '21 at 00:38
3

Use a string as a parameter to avoid that weird behavior of Date constructor.

Example:

const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
Crepkey
  • 386
  • 1
  • 4
  • 8
0

In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)

just always write

new Date(yea,month - 1,seconds ,millisecond)