15

This caused me a bit of a headache last night and I wanted to understand why the getDate method in the Date object is 1 based (returns values from 1-31) while the getMonth method is 0 based (returns 0-11). I'm wondering why there is this inconsistency in methods for the same object.

I understand why it's difficult to change the behavior now but are there any reasons this was designed like this in the first place?

Documentation can be found here: http://www.w3schools.com/jsref/jsref_obj_date.asp

Dan Goldin
  • 957
  • 13
  • 32
  • 1
    Related: http://stackoverflow.com/questions/834757/why-does-getday-return-incorrect-values-javascript/834807#834807 – Guffa Mar 13 '12 at 16:02
  • Drop him a tweet and ask him, I would love to hear his response, it caught me out too! http://twitter.com/#!/brendaneich (p.s. when I say 'him' I mean the creator) – mbx-mbx Mar 13 '12 at 16:10
  • rel: http://stackoverflow.com/questions/1453043/zero-based-month-numbering – georg Mar 13 '12 at 16:31
  • http://programmers.stackexchange.com/questions/108495/why-getmonth-starts-with-0 – Bergi Mar 26 '15 at 00:08

4 Answers4

23

So I dropped Brendan Eich a tweet asking him the question (for those who don't know he is the creator of JS) and his response was:

@magrangs because that is how java.util.Date did it.

https://twitter.com/BrendanEich/status/179610205317902337

Noyo
  • 4,874
  • 4
  • 39
  • 41
mbx-mbx
  • 1,765
  • 11
  • 23
  • 3
    Ok, new question: why did Java do it that way? ("Because that's how C did it!"... Ok, why did C do it that way? ...???) – Noyo Oct 09 '14 at 09:28
  • 1
    @Noyo Because using a zero-based count makes it possible to index into an array. http://stackoverflow.com/questions/9687521/9687586#9687586 – Tomalak Jan 04 '15 at 06:58
14

I suppose months are 0-based because Java did it the same way when the JavaScript language was designed.

EDIT Oracle took down older Java documentation, there is an archived version of that page.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    I guess my question turns into why was it implemented that way in Java? – Dan Goldin Mar 13 '12 at 16:06
  • @DanGoldin: I was anticipating that response. And I could only guess: Because they decided it would be confusing to return `12` from `getDate()` when the current date is `13`. And they also decided that `for (m=0; m<12; m++)` would look nicer than `for (m=1; m<13; m++)`. Or something like that. – Tomalak Mar 13 '12 at 16:15
  • 4
    @Tomalak: It looks as if you're correct based on the response from Brendan Eich - https://twitter.com/#!/BrendanEich/status/179638412775661572 – Dan Goldin Mar 14 '12 at 05:07
  • Oracle, 2014: We're sorry, the page you requested was not found. Case in point. :-) – miraculixx Jun 07 '14 at 13:32
  • @Tomalak the link is down. – Persijn Jun 22 '15 at 09:51
6

Hard to tell, but I have a suspicion that Month is 0-based so it can be easily used as an indexer in a months array that holds month names

var months = new ["Jan", "Feb", ....]
months[new Date().getMonth()];
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 1
    This is a pretty interesting explanation - thanks! I would still consider it bad design to implement it to be 0 based because you suspect people would want to map it to an array. I think it would be better to keep it at 1 and then have the users decide to subtract 1 if they want to index them in the array. – Dan Goldin Mar 13 '12 at 16:08
  • I'm not saying it's a good design - I just think that *may* have been the reason. – Jakub Konecki Mar 13 '12 at 16:23
  • 1
    The dates can as well! `var dates = [1,2,3...31]` – jjt Nov 28 '13 at 22:26
  • 1
    This would be consistent with `getDay` which is also zero-based and likely to be used for array indexing. – marsze Feb 29 '16 at 08:29
4

In JavaScript, counters start at zero.

Months do not necessarily have to be represented by a digit. "Months" is a countable sequence. The first element of this sequence is referred by zero.

In real-life, days are represented by a fixed digit. Although days are also countable, it would be extremely confusing to represent the first day as Day Zero.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 2
    *"Months do not necessarily have to start at 1"* - That's a statement I find difficult to understand. – Tomalak Mar 13 '12 at 16:01
  • @Tomalak By that, I mean that months can be represented by "January", etc. I can't think of any other method to represent the days of the month. The days of a week, on the other hand, can also be represented by "Sunday", etc. It also starts counting at zero. – Rob W Mar 13 '12 at 16:05
  • But in real-life, months are represented by a fixed digit as well. – Dan Goldin Mar 13 '12 at 16:08
  • @DanGoldin Read "do not necessarily have to start at 1" as "do not necessarily have to be represented by a digit". – Rob W Mar 13 '12 at 16:10
  • 5
    "Weeks start with Sunday" is also something I find very difficult to understand. ;) Anyway, by that argument, you could say that there is a month `0` labelled `'January'`, a weekday `0` labelled `'Sunday'` and a date `0` labelled `'1'`, the label - by chance - being a digit. At least that would have been *consistent*. It clearly doesn't make a lot of sense the way it is, it's just "one of those things". – Tomalak Mar 13 '12 at 16:11
  • Just to note that week start with Monday, of course. -1 for UScentrism. – mcepl Oct 23 '15 at 05:11
  • @mcepl - That's hardly a US-centric thing. Lots of cultures count Sunday as the first day of the week. Even the Bible does (sort of), and it predates the USA by rather a long time. – nnnnnn Jul 10 '17 at 04:56