DEV Community

Cover image for Vanilla JavaScript get Month Name
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Vanilla JavaScript get Month Name

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

JavaScript getting a Months name

So let's start off by defining a date object.

var date = new Date();
// Todays date
Enter fullscreen mode Exit fullscreen mode

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

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

Instead of the long option we can also use short and get a Month like: Dec

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

And as you have seen we are providing a default keywords this is the placeholder for the locale so let's try and get a months name in a different locale.

var december = new Date('12/01/2020');
console.log(december.toLocaleString('fr-FR', {month: 'long'}));
// décembre
Enter fullscreen mode Exit fullscreen mode

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

This is a widely supported method, feel free to use it.

Date toLocalString

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

Top comments (0)