javascript

Get month name from a date using javascript

To get the month name from a given date you can use these code examples of javascript. You can get a full month name or short month name using these methods.

const date_str = "2021-09-20";
const my_date = new Date(date_str);

//Get month name from date using .toLocaleString() method
const full_month_name = my_date.toLocaleString("default", { month: "long" });
// -> September

//Get month name from date using .getMonth() method
const month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const month_num = my_date.getMonth();
const month_name = month_names[month_num];
// -> September

In the above code example, we are given date in string format. Firstly, we are converting this date string to a javascript date object and then using two methods to get the month name from the date object.

In the first method we are using .toLocaleString() method and in the second code example we are using .getMonth() method of javascript date object.

Live Demo

const date = "2020-06-28";
const date_obj = new Date(date);

const month_name = date_obj.toLocaleString('default', {month: 'short'});
console.log(month_name);
If you want to get the short name of the month from a given date you can use the above code. Here we are assigning the 'short' value to the month key when calling the .toLocaleString() method. You can check the demo for the above code here.
const date = "2018-08-28";
const date_obj = new Date(date);

const month_name = date_obj.toLocaleString('bg', {month: 'long'});
console.log(month_name);
// -> август
Here, we are using 'bg' as language code when getting month name from date. You can check demo for the above code here.
Was this helpful?