How to Get the Month Name from a Date in JavaScript?

To get the month name from a date in JavaScript, call the toLocaleString() method on the date object by passing the first parameter as 'default' and the second parameter as {month: 'long'} to get the full name of the month from the specified date.

For example,

// Current date
const date = new Date()

// Get the month name
const month = date.toLocaleString('default', {month: 'long'});

console.log(month);
// Output: 👉 "March"

If you want to get the shorter version of the month name such as Jan, Feb, Mar, etc., replace 'long' with 'short'.

For example,

// Current date
const date = new Date()

// Get the month name
const month = date.toLocaleString('default', {month: 'short'});

console.log(month);
// Output: 👉 "Mar"

If you already have a date string and you want to get the month name from it, you have to first pass the date string to the Date() constructor as an argument to get the date object, and then call the toLocaleString() method on the date object to get the month name.

For example,

// Jan 10, 2023
const date = new Date('2023-01-10');

// Get the month name
const month = date.toLocaleString('default', {month: 'long'});

console.log(month);
// Output: 👉 "January"

Similarly, if you want to get the shorter version of the month name from the specified date, you can replace ‘long’ with ‘short’.

For example,

// Jan 10, 2023
const date = new Date('2023-01-10');

// Get the month name(shorter version)
const month = date.toLocaleString('default', {month: 'short'});

console.log(month);
// Output: 👉 "Jan"

2. Get the Month Name using the getMonth() Method

The getMonth() method returns an integer between 0 and 11 representing the month of the year for the specified date. Value 0 corresponds to January, 1 corresponds to February, and so on.

In this method, we define a custom array containing each month’s name and then use the getMonth() method to get the index of the month for the given date.

For example,

// All months names
const MONTHS = [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];

// 14 Feb, 2023
const date = new Date('2023-02-14');

// Returns the month index(0 based)
const monthIdx = date.getMonth();

// Get the month name
const monthName = MONTHS[monthIdx];

console.log(monthName);
// Output: 👉 "Feb"

You can use either of the two methods to get the month name from a given date.

The first method is pretty straightforward, you can simply call the toLocaleString() method on the date object with the desired parameters to get the month name.

The second method on the other hand a bit lengthy. You have to first define an array with each month’s name and then use the month index to get the month name.

No doubt, you can go with any of the two methods whichever you like.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.