JavaScript: Get the name of a month.

In this guide, we will show you how to get the textual name of a month using JavaScript.

In JavaScript, we use the getMonth() method to return the numerical representation of a month.

However, this is a numerical value that starts at 0, where January is 0, February is 1, and so forth.

As a result, we will need to manually convert this number into its textual equivalent.

How to get the name of a month using JavaScript.

Take a look at the following function, which you can copy and paste into your project:

/**
 * A JavaScript function that will convert the numerical
 * index of a month into its textual equivalent
 *
 * @param monthIndex int The result of the getMonth() method.
 * @returns string
 */
function getMonthName(monthIndex){
    //An array containing the name of each month.
    var months = [
        "January", "February", "March", "April", "May",
        "June", "July", "August", "September", "October",
        "November", "December"
    ];
    return months[monthIndex];
}

The JavaScript function will take in the result of the getMonth() method and then convert that number into the name of the month.

For example, if you pass 0 in as the parameter, then this function will return the string “January”. If you pass in the number 11, then it will return “December”.

Let’s take a look at a few examples of how you can use this function.

Getting the name of the current month.

If you want to get the name of the current month, then you can use the function like so:

//Getting the current month name.
var date = new Date();
var monthNum = date.getMonth();
console.log(getMonthName(monthNum));

As you can see, all we had to do here was pass in the result of the getMonth() method.

When we ran the code above, the function returned the string “June”.

Given date.

If you are looking to get the month name from a given date, then it is pretty much the same as the previous example:

//Getting the month name of a given date and time.
var date = new Date('1995-12-17T03:24:00');
var monthNum = date.getMonth();
console.log(getMonthName(monthNum));

The only difference here is that we passed a specific date into the constructor of our Date object.

Next month.

This function will work with any Date object.

For example, let’s say that we want to get the name of next month:

//Get the textual name of next month.
var currentDate = new Date();
//11 = December
if (currentDate.getMonth() == 11) {
    //January is 0
    var nextMonth = new Date(currentDate.getFullYear() + 1, 0, 1);
} else{
    var nextMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
}
var nextMonthIndex = nextMonth.getMonth();
//Print out the month name
console.log(getMonthName(nextMonthIndex));

Here, we manually work out what the number of the next month will be before passing the result into our getMonthName function.

Using the toLocaleString method.

You can also use the toLocaleString() method:

var currentDate = new Date();  //Current date
var month = currentDate.toLocaleString('default', { month: 'long' });
console.log(month);

This will work fine in most modern browsers.

However, you may run into the following issues:

  1. The toLocaleString() method can be pretty slow. As a result, you can not use it on a lot of dates.
  2. Older browsers might not support it.

See also: Get the day of the week using JavaScript.