Subscribe

Vanilla JavaScript get Month Name

✍️

Learn in this guide how to get a localized month name from a date object in JavaScript. See the code examples in the Codepen!

22 May, 2020 Β· 2 min read

Sticking to the date theme, as we have seen how to get the days between two dates yesterday. Today we are going to learn how we can get a month's name in JavaScript.

How to get the month name in Javascript

So let's start off by creating a date object:

const date = new Date();
// Todays date

We can use the JavaScript function toLocaleString to get a months name.

console.log(date.toLocaleString('default', { month: 'long' }));
// May

Instead of the long option we can also use the short option and get the month name like for instance Dec

const december = new Date('12/01/2020');
console.log(december.toLocaleString('default', { month: 'short' }));
// Dec

And as you have seen we are providing the default keyword. This is the placeholder for the locale.

So let's get a months name in a different locale with this code:

const december = new Date('12/01/2020');
console.log(december.toLocaleString('fr-FR', { month: 'long' }));
// dΓ©cembre

As you can see, now we got the months name from the date object for a different country/language.

See the code examples in this Codepen

I hope you found this a useful tip and feel free to check out the Codepen.

See the Pen Vanilla JavaScript get Month Name by Chris Bongers (@rebelchris) on CodePen.

Browser Support

The toLocaleString method is a widely supported method. Feel free to use it.

Date toLocalString browser support

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next πŸ“–

JavaScript sending data between windows

9 Sep, 2022 Β· 4 min read

JavaScript sending data between windows

Using the native payment request JavaScript API

9 Aug, 2022 Β· 8 min read

Using the native payment request JavaScript API

Join 1829 devs and subscribe to my newsletter