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);
const date = "2018-08-28";
const date_obj = new Date(date);
const month_name = date_obj.toLocaleString('bg', {month: 'long'});
console.log(month_name);
// -> авгуÑÑ‚
0 Comments