You can simply use the toLocaleString() method to get the month name from a date in JavaScript.
This method is supported in all major modern browsers. Let's take a look at an example:
// Creating a date object
var today = new Date();
// Getting full month name (e.g. "June")
var month = today.toLocaleString('default', { month: 'long' });
console.log(month);
Similarly, you can get the month name from a date string as shown in the following example:
// Creating a date object
var today = new Date('2021-10-06'); // yyyy-mm-dd
// Getting short month name (e.g. "Oct")
var month = today.toLocaleString('default', { month: 'short' });
console.log(month);
To specify options but use the browser's default locale, use 'default'. Possible values for month property are "numeric", "2-digit", "narrow", "short", "long".
Use the
toLocaleString()
MethodYou can simply use the
toLocaleString()
method to get the month name from a date in JavaScript.This method is supported in all major modern browsers. Let's take a look at an example:
Similarly, you can get the month name from a date string as shown in the following example:
To specify options but use the browser's default locale, use 'default'. Possible values for month property are "numeric", "2-digit", "narrow", "short", "long".
need an explanation for this answer? contact us directly to get an explanation for this answer