How To Get The Month Name From A Date In JavaScript

Get the month name from a date in javascript

There are some ways to get the month name from a date in JavaScript. In this article we’ll discuss about the toLocaleString(), Intl.DateTimeFormat and create a function to get the month name. Let’s learn about it with the explanation and examples below.

Get the month name from a date in JavaScript

Use the toLocaleString() method

The toLocaleString() method returns a date object like locale settings.

Syntax

toLocaleString(locales, options) 

Parameters

  • locales: (Optional) What language format to use?, ex: Australian English is ‘en-Au’, Canada English is ‘en-CA’.
  • options: (Optional) You can set some properties in the object.

Return Value: A string.

Example: 

// Create a date object to get date time today
const today = new Date();

// Get date time of US
const usDate = today.toLocaleString("en-US");

// Get long month name
const longMonthName = today.toLocaleString("en-US", { month: "long" });

// Get short month name
const shortMonthName = today.toLocaleString("en-US", { month: "short" });
 
console.log(usDate);
console.log('The long name of the current month is: ', longMonthName);
console.log('The short name of the current month is: ', shortMonthName);

Output

9/18/2022, 7:35:36 PM
The long name of the current month is: 
September
The short name of the current month is: 
Sep

Use the Intl.DateTimeFormat object in JavaScript

The Intl.DateTimeFormat.prototype.format() method is an built-in method in JavaScript.

Syntax

Intl.DateTimeFormat.format(date)

Parameters:

  • date: The Date needs to format.

Return Value: The formatted date.

Example:

var longMonthName = new Intl.DateTimeFormat('en-US', {month: "long"}).format(new Date());
var shortMonthName = new Intl.DateTimeFormat('en-US', {month: "short"}).format(new Date());
 
console.log('The long name of the current month is: ', longMonthName);
console.log('The short name of the current month is: ', shortMonthName);

Output

The long name of the current month is: 
September
The short name of the current month is: 
Sep

Create a function to get the month name from Date

Example

In this example, we will create the monthName array with month name values. Then we use the getMonth() method to get month of the current day. The getMonth() method returns the month of the Date (0 to 11), so we can call the month name from the monthName array.

const monthName = ["January", "February", "March", "April", "May", "June",
       	"July", "August", "September", "October", "November", "December"
];

getMonthName = function(month) {
	return monthName[month.getMonth()];
}

console.log('The long name of the current month is: ', getMonthName(new Date()));

Output

The long name of the current month is: 
September

Summary

Our tutorial has guided you different ways to get the month name from a date in JavaScript. We always hope this tutorial is helpful to you. If you have any questions, please let us know by leaving your comment in this tutorial. Thank you.

Maybe you are interested:

Leave a Reply

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