JavaScript – Get Month Name from Date

featured Image

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:

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

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *