0

Possible Duplicate:
Zero-based month numbering

The explanation in the book I am reading says, "Well, my Aunt starts counting her months from 1 like everyone else, so we subtract 1.

Here's the input string date: "died 27/04/2006: Black Leclère"

Here's the code:

function extractDate(paragraph) {
  function numberAt(start, length) {
    return Number(paragraph.slice(start, start + length));
  }
  return new Date(numberAt(11, 4), numberAt(8, 2) - 1,
                  numberAt(5, 2));
}

alert(extractDate("died 27-04-2006: Black Leclère"));

04 - 1 is 03. But here's the output date object: Thu Apr 27 2006 00:00:00.

Knowing what we want for the output doesn't explain the unusual behavior of the language and the subsequent necessity of the -1. Please, explain.

Community
  • 1
  • 1
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78
  • 2
    The Aunt counts from one, but Uncle JavaScript counts from 0. See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date – Yoshi Oct 20 '11 at 10:05
  • @Yoshi Why does it start from 0 when counting months but start from 1 when counting years? – Wolfpack'08 Oct 20 '11 at 10:06
  • Oh my Gosh. That's ridiculous. Why would they do that? It's the Christian calendar. There is no 0 for months. They should break the Internet for that one, imo. I can't believe the author just threw it out there all nonchalant like it's to be expected. Well, thanks guys. – Wolfpack'08 Oct 20 '11 at 10:07
  • 1
    I guess it's because the month range is fixed (12 elements), whereas the year range is open to both ends (theoretically). – Yoshi Oct 20 '11 at 10:09
  • @Yoshi - in fact with years there isn't a year zero (at least in our calendar), so it's not a straightforward open ended at both ends; there is a numerical quirk at the AD/BC transition. – Spudley Oct 20 '11 at 10:16
  • @Spudly ;) I actually just meant what you wrote in your answer, though didn't find the right words for it. – Yoshi Oct 20 '11 at 10:20
  • As Yoshi suggested with [this link](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date), Javascript numbers the months 0-11, with 0 being January. So, if the input is 04, the date output object is May, and if the input's 03, the date object output is April. – Wolfpack'08 Oct 20 '11 at 10:10
  • Bah. My title's better. – Wolfpack'08 Oct 20 '11 at 12:20

2 Answers2

4

Months represented in JavaScript Date objects are 0-based. That is, January is represented as 0, February as 1, March as 2, etc.

For some interesting answers as to why this behaviour is implemented across many languages, see Zero-based month numbering.

"the combination of zero-based arrays and 1-based month values does bring a problem. To get the month name of month 9, you would have to get item 8 from the month array. Some developers would be happy with decreasing the month number before getting it's name. Others preferred to change the month into something zero-based since people just want to know the name, not the number. It's a personal view."

Community
  • 1
  • 1
Andy E
  • 338,112
  • 86
  • 474
  • 445
1

I think the technical reason it does this would be because month is an enumerated field, meaning that it can be mapped to an array of month names, and arrays count from zero, whereas years and days are not enumerated; you always just want the actual number value, so that's what is stored.

It is a perfectly valid question to ask though, because if you're using months as a number then it can seem a bit weird (not to mention easy to forget).

By the way, if you're working with dates, particularly extracting dates from strings and formatting them into strings, you might want to look into Date.js, which is a javascript library that can help a lot with all this kind of thing.

Spudley
  • 166,037
  • 39
  • 233
  • 307