javascript

Get the current month name using javascript

To get the current month name if you are using javascript, you can use ECMAScript .toLocaleString() method or .getMonth() method of javascript. The different ways to get the current month name are described below.

var today_date = new Date();

// Using toLocaleString(Ecmascript)
// Full month name
const full_month_name = today_date.toLocaleString('default', { month: 'long' });
console.log(full_month_name);
// -> October(Or the current month)

//Short month name
const short_month_name = today_date.toLocaleString('default', { month: 'short' });
console.log(short_month_name);
// -> Oct (Or the short version of current month)

//Using getMonth() method
const month_num = today_date.getMonth();
const all_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const current_month = all_months[month_num];
console.log(current_month);
// -> October (Or the curent month name)

In the code snippet, we are using the .toLocaleString() method of javascript date which takes two parameters. The format of the month name that you want to get and the second parameter is an object which takes key values as the options passed to get the month.

As an alternative, we can also use the .getMonth() javascript date method which returns the month number in integer format.

Live Demo

//FUll Month name
const full_month_name = new Date().toLocaleString('default', { month: 'long' });
// -> returns full current month name  e.g. October

const short_month_name = new Date().toLocaleString('default', { month: 'short' });
// -> returns short current month name  e.g. Oct
In the above code we are using new Date().toLocaleString() method. It takes two parameters. In the first parameter, you can pass in which language you want to get the month name. You can pass different language codes e.g. de-DE in this. In the second parameter, you can pass multiple options as if you want the full or short month name etc.
const month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];

const month = new Date().getMonth();
const month_name = month_names[month];
// -> returns name of month
Date.getMonth() method of Javascript date can also be used to get the current month. But here you need to create the full month array first. The .getMonth() returns the month in numeric form and its starts from 0. You can now get the month name using this integer as an index for months_names array.
Was this helpful?