Counting months from zero

Posted on

Many programming languages start counting months one number before we would. It’s like saying January is “month zero” and December is “month eleven”.

const months = [
  "January", "February", "March",     "April",   "May",      "June",
  "July",    "August",   "September", "October", "November", "December"
]
 
// `.length` tells us how many months there are in a year.
months.length // ⇒ 12
 
// Counting in an array start at 0, so the months are numbered from 0 to 11.
// A year still has 12 months, but we need to shift their index down by 1.
 
// While January is the 1st month for us, it’s the 0th element in the array.
months[0] // ⇒ "January"
 
// The index of December is also one less than what we would say.
months[11] // ⇒ "December"
 
// The month at the index 12 would be the 13th month, which does not exist.
months[12] // ⇒ undefined
 
const iPhoneReleaseDate = new Date('June 29, 2007');
iPhoneReleaseDate.getMonth()
// ⇒ 5 (because June is the 6th month)
const months = [
  "January", "February", "March",     "April",   "May",      "June",
  "July",    "August",   "September", "October", "November", "December"
]
 
// `.length` tells us how many months there are in a year.
months.length // ⇒ 12
 
// Counting in an array start at 0, so the months are numbered from 0 to 11.
// A year still has 12 months, but we need to shift their index down by 1.
 
// While January is the 1st month for us, it’s the 0th element in the array.
months[0] // ⇒ "January"
 
// The index of December is also one less than what we would say.
months[11] // ⇒ "December"
 
// The month at the index 12 would be the 13th month, which does not exist.
months[12] // ⇒ undefined
 
const iPhoneReleaseDate = new Date('June 29, 2007');
iPhoneReleaseDate.getMonth()
// ⇒ 5 (because June is the 6th month)
Debug
none
Grid overlay