JavaScript - Get Month Name from Date


JavaScript - Get Month Name from Date

In this post, we will see how to get the string value of the month name from the JavaScript Date object instance.

In JavaScript, we get the getMonth() method to get the month from JavaScript Date Object Instance.

However, it only returns the index of the month starting from 0 and not the actual alphabetic name of the month.

So, to get the month name from JavaScript Date:

  1. Use toLocaleString() method.
  2. Use Intl.DateTimeFormat Object

Now, let's see how to get the month name with an example in JavaScript.

Use toLocaleStirng() method to get the month name

To get the month name, we can use the toLocaleString() method in JavaScript. It returns a string with language-sensitive representation of the Date Object.

The default language depends on the local setup of your computer.

Now, let's use the method to get the month.

const date = new Date()
const month_name = date.toLocaleString('default', {month:'long'})

console.log(month_name)

Output:

August

We can also use the short format like this:

date.toLocaleString('default', {month:'short'}) // Aug

Using the Intl.DateTimeFormat Object

We can also use the Intl.dateTimeFormat method to get the name of the month from the Date.

It provides a format() function where we pass the date object and it returns converted language-sensitive data-time formats.

const date = new Date();
const month = new Intl.DateTimeFormat('en-US', {month: "long"}).format(date);

console.log(month) // August

Here again, we can use the short format like this:

new Intl.DateTimeFormat('en-US', {month: "short"}).format(date); // Aug

Conclusion: This is how we can get the month name from Javascript date using the toLocaleStirng() method and Intl.DateTimeFormat object.


Related Topics:

Add Days to a Date in JavaScript with Example

Get Tomorrow's Date using JavaScript - Quick Way

Get Current Date in Different Date Formats in JavaScript

Code on how to get Yesterday date in JavaScript

Convert date to long date format using Javascript