Get Month Number from Month Name in JavaScript

avatar

Borislav Hadzhiev

Mon Oct 25 20211 min read

banner

Photo by Cody Board

Get Month Number from Month Name #

To get the month number from a month name, create an object containing the month names as keys and the month numbers as values. You can then use bracket notation [] to get the month number by a month's name.

index.js
const monthsShort = { Jan: '01', Feb: '02', Mar: '03', Apr: '04', May: '05', Jun: '06', Jul: '07', Aug: '08', Sep: '09', Oct: '10', Nov: '11', Dec: '12', }; console.log(monthsShort['Aug']); // ๐Ÿ‘‰๏ธ 08 const monthsLong = { January: '01', February: '02', March: '03', April: '04', May: '05', June: '06', July: '07', August: '08', September: '09', October: '10', November: '11', December: '12', }; console.log(monthsLong['August']); // ๐Ÿ‘‰๏ธ 08

We created 2 objects that store the names of the months as keys and the numbers of the months as values.

The examples include both a 3 letter abbreviation for the month names and the full name of each month.

You can then use bracket notation [] to get the month number for a specific month name.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee