1: posts
Reading ModeRO
$ cat 20211014_getmonth.md

#JavaScript’s getMonth returns a zero-based index

Date:2021-10-14 (Thu)
>A date bug led me to investigate what getMonth returns and why January is represented by zero.

##Table of Contents

##Introduction

I ran into an easy-to-miss detail of JavaScript’s Date object.
I suspect most people have tripped over it at least once. I’m writing it down for my own reference.
Understanding the convention makes it easier to spot the mistake next time.

##The getMonth trap

The method that caught me out was getMonth on JavaScript’s Date object.
Date is the object JavaScript provides for working with dates and times.
For example, this creates a Date for the current moment:

JavaScript
const now = new Date();

console.log(now);
// Thu Oct 14 2021 22:18:27 GMT+0900 (日本標準時)

A Date has methods for reading individual components, such as its year, month, or day.
There’s getFullYear for the year, getDate for the day of the month, and getHours and getMinutes for the time.
The Date documentation lists the other methods.

JavaScript
console.log(now.getFullYear());
// 2021

console.log(now.getDate());
// 14

console.log(now.getHours());
// 22

Each of those values matches what I’d expect.

Now for the part I wanted to investigate.
What happens when I ask the same Date for its month?
The documentation lists a method called getMonth.
Let’s try it:

JavaScript
console.log(now.getMonth());
// 9

Wait a second.
It was October when I ran this, yet the result was 9.
That’s the detail I missed.
Until I noticed it the other day, every post on this blog showed a date one month off.

In Python’s datetime library, which I use a lot, months are numbered 1 through 12.
I’d assumed JavaScript would follow the same convention.

##Why are months numbered this way?

The documentation explicitly describes the return value as zero-based:

getMonth() gives the month in local time, counting from 0 for the first month of the year.

By contrast, getDate returns the day of the month as a number from 1 to 31. That’s a different convention from getMonth.
I couldn’t find a definitive explanation for the design, but I came across two theories:

  • In many European languages, people refer to months by name.
  • JavaScript followed a convention used by older libraries, including C’s.

The first explanation helped me make sense of it.
In Japanese we typically write months using numbers: 1月, 2月, and so on. In English we say January, February, and so on.
Put the English names in an array, and the result of getMonth selects the right name directly:

JavaScript
const now = new Date();

// English month names
const eng = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December',
];

console.log(eng[now.getMonth()]);
// October

That’s quite natural to write.
Swap out the array and the same code works for another language:

JavaScript
// Italian month names
const italy = [
  'Gennaio',
  'Febbraio',
  'Marzo',
  'Aprile',
  'Maggio',
  'Giugno',
  'Luglio',
  'Agosto',
  'Settembre',
  'Ottobre',
  'Novembre',
  'Dicembre',
];

console.log(italy[now.getMonth()]);
// Ottobre

Once I thought of getMonth as returning an index into a list of names, starting at zero stopped feeling strange.
The day of the month, on the other hand, is normally a number even in English, as in “14th October, 2021.”
Perhaps that’s why getDate gives the number itself.
I wondered whether this convention would seem less surprising to someone used to writing dates that way.

Japanese actually has a traditional set of month names too.
They’re called wafū getsumei.

JavaScript
const jpnOld = ['睦月', '如月', '弥生', '卯月', '皐月', '水無月', '文月', '葉月', '長月', '神無月', '霜月', '師走'];

console.log(jpnOld[now.getMonth()]);
// 神無月

In a world where people still used those names every day and somehow also had JavaScript, perhaps getMonth wouldn’t have tripped anyone up.

##Wrapping up

That was the detail of JavaScript’s getMonth method that caught me out.
The convention makes more sense to me when I think of the result as an index into an array of month names.
I’ll keep that distinction in mind the next time I use getMonth.

## Comments

"20211014_getmonth.md" [readonly]1L, 4490B
utf-8100%