91

This question is purely to satisfy my curiosity.

In the JavaScript Date object, when you call getMonth() it returns the month but it counts from 0.

0 = January
1 = February 
...

But when you call getDate() it starts counting from 1

1 = 1
2 = 2
... 

Why the inconsistency?

SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
PrimeLens
  • 2,547
  • 4
  • 23
  • 28
  • but getDate() returns day of month, not month... – jsedano Apr 03 '13 at 22:43
  • then again getDay() Returns the day of the week (from 0-6) ... – jsedano Apr 03 '13 at 22:44
  • 10
    I'd guess because a day of the month is actually a *value*, where the month is more of an enumerable type (eg, you might want to map it to an array of month names). But it could also just be that dates in JavaScript are a gigantic mess and can never be changed. – Mike Christensen Apr 03 '13 at 22:45
  • 1
    Good question. But I don't think that there is a answer to this that makes sense, that's just how things are. – jgillich Apr 03 '13 at 22:46
  • 2
    This is most confusing in the constructor new Date(2017, 1, 1), which is the 1st of February 2017. – Richard Garside Sep 19 '17 at 09:06
  • Related: https://stackoverflow.com/questions/2552483/why-does-the-month-argument-range-from-0-to-11-in-javascripts-date-constructor – General Grievance Feb 05 '21 at 20:42

4 Answers4

85

I assume it's because it would be easier to reference in an array of names, i.e.

var months = ["January", "February", "March", "April", "May", "June", "July",
         "August", "September", "October", "November", "December"];

var d = new Date();

var namedMonth = months[d.getMonth()];

If getMonth() returned 1-12, then programmers would have to do d.getMonth()-1 everytime they wanted a fancy named month.

Days of the month don't have specific "names" per se. The getDate() returns 1-(28-31). We usually just refer to them by their number.

The same concept as getMonth() applies for getDay() also, which returns 0-6 based on the day of the week

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var namedDay = days[d.getDay()];

All this returns something like:

console.log("Month: month[" + d.getMonth() + "]: " + namedMonth); 
//Month: month[3]:  April
console.log("Day: days[" + d.getDay() + "]: " + namedDay); 
// Day: days[4] : Thursday 
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
  • 11
    How does this explain why `.getDate()` starts at `1`? – Blender Apr 03 '13 at 22:47
  • He's asking about `.getDate()` and not about `.getDay()` – Karol Apr 03 '13 at 22:58
  • 7
    javascript sucks if it was really made that way by this reason. For me it would be easier to do: var months=['', 'Jan', ...]; than remember that getMonth can return 0. – Stanislav Jan 15 '15 at 19:57
  • @Stanislav a lot of languages are like that...JavaScript isn't the first – SomeShinyObject Jan 16 '15 at 00:11
  • 1
    What's this about Japanese…? :) – deceze Apr 27 '17 at 02:42
  • They're still using the same numbers in writing though, which is why that comment is a bit odd… :) – deceze Apr 27 '17 at 03:17
  • 1
    *If `getMonth()` returned 1-12, then programmers would have to do `d.getMonth()-1` everytime they wanted a fancy named month.* --- Doesn't seem logical. Because if programmers want to have the month number, then they have to do `d.getMonth()+1` – AliN11 Feb 14 '21 at 09:24
6

Coming a bit late to this, but the correct answer is here:

https://stackoverflow.com/a/41992352/134120

They (the creators of JavaScript) copied the functionality from the corresponding class in Java (which in turn seems to have been copied from C). And so we're propagating the mistakes of the past ‍♂️

AsGoodAsItGets
  • 2,886
  • 34
  • 49
0

If you want to say it's inconsistency - you need to ask the creator of specification of language. According to this page JavaScript is based on ECMAScript (EDIT: see @MichaelGeary comment).

And when you read from page 165 here, you will see that all is working exactly as it's designed.

For you it can be inconsistency. For me it's rather a feature - 0-based values let you access Array straight away without doing calculations (see @Christopher's answer). In case of day of month you can't really access any Array. It will be weird to have Array of names of days of the month... like this:

var namesOfDays = [
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", // and again at least 4 times ...
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
    "Sunday", "Monday", "Tuesday"
]
Karol
  • 7,803
  • 9
  • 49
  • 67
  • 9
    You're right that this behavior is "as designed", but you have your history wrong. JavaScript is not based on ECMAScript, it's the other way around: [JavaScript was developed in 1995 and submitted to Ecma for standardization the next year](http://en.wikipedia.org/wiki/JavaScript#History). The ECMAScript standard was written to describe JavaScript as it actually existed. The reason the standard says what it says is that's how JavaScript already worked. – Michael Geary Jun 13 '13 at 16:40
0

Link1

Date.prototype.getDate()
Returns the day of the month (1-31) for the specified date according to local time.

Link2

A Date object contains a number representing a particular instant in time to within a millisecond For example, if you specify 150 seconds, JavaScript redefines that number as two minutes and 30 seconds.

When you implement methods in Javascript to find the difference between two times specified in miliseconds, you would need to return a date which needs to be greater than 0 for obvious reasons.

var startTime = new Date('1/1/1990');  
var startMsec = startTime.getMilliseconds();  
startTime.setTime(5000000);  
var elapsed = (startTime.getTime() - startMsec) / 1000;   
document.write(elapsed);  

// Output: 5000  

As explained by "SomeShinyObject" that

var months = ["January", "February", "March", "April", "May", "June", "July",
         "August", "September", "October", "November", "December"];

helps in referencing them through array index.

Hence getDay, getHours, getMonths starts from 0.

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
  • 3
    I don't understand what your talk about dates represented as a number of milliseconds or finding the difference between two dates has to do with this question. – nnnnnn Jul 10 '17 at 05:01