10

When creating a new Date object using numbers for the parts, the value I get back is exactly one month ahead of the value I put in for 'month'.

new Date(2012, 05, 17, 00, 00, 00)
Sun Jun 17 2012 00:00:00 GMT+0800 (HKT)  // june?!

However, a normal parse of exactly the same string returns the correct time:

new Date("2012-05-17 00:00:00")
Thu May 17 2012 00:00:00 GMT+0800 (HKT)

I get the same result in ie/ff/chrome. Removing hours/min/seconds doesn't have any effect. I can work around it by subtracting one before setting the month, but instead I just switched to writing out my date as a string.

Edit: The string parse doesn't work in IE. I have no idea what I did, but I swear I made that work. Thats prob. why I avoided it in the first place. I've switched to using moment.js for now.

Ah, now I get it. Just like regular java dates, which I don't code in except rarely, and even then always with a library (joda, etc). What a terrible idea anyway. Here is skeets take on the question: Why is January month 0 in Java Calendar?

Why is this happening?

Community
  • 1
  • 1
Andrew
  • 8,322
  • 2
  • 47
  • 70
  • 2
    It's worth noting that 05 is actually an octal number. Luckily octal 5 == decimal 5. – Corbin May 18 '12 at 08:13
  • 2
    Also, just for the sake of referencing something official-ish: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date. Month: "Integer value representing the month, beginning with 0 for January to 11 for December." – Corbin May 18 '12 at 08:15
  • Thanks @corbin. I caught that, but didn't include it in the examples. Our standard way of formatting stuff coming out of .net is MM, so it slipped by. Lucky me it wasn't 09 :) – Andrew May 18 '12 at 09:38
  • 1
    Actually since 09 isn't valid octal, I believe that 09 === 9 (basically as an 'oops' guard). Really the 0 is harmless unless you try to do 011 or 012. Wasn't really meant to read as a correction, but rather just a side note :) – Corbin May 18 '12 at 09:39
  • Ah :) I've never tried 09, no clue. I don't do much JS. – Andrew May 19 '12 at 08:21

1 Answers1

17

Programmers start counting from 0. So months are represented by 0(Jan)-11(Dec).

The reason days don't follow this rule is to not confuse authors with 30/31 month differences.

From MDN:

month

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

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 2
    Ah, there is the reference I was looking for. Googling for this combo of information just doesn't help. But for FFS, who on *earth* thought this would be _less_ confusing? _people_ start counting from 1 though, and I have never seen a library do this any language from basic to java to ruby to perl, to now, c#. Sigh. Yes i'm a programmer, I figured this was why, but it seems so *stupid*. – Andrew May 18 '12 at 08:25
  • I honestly guess this is just a convenience thing internally to make the math when creating date objects easier. Otherwise there is no sane reason. Up until now i've always written the dates out as seconds and cast to (date), so I've never noticed this. – Andrew May 18 '12 at 08:27
  • I do not get the reason why days do not follow the rule. I think it is just one of the many design flaws in javascript. – Alex Jun 21 '16 at 18:59