You can get the name of the month from a date in JavaScript by using the toLocaleString()
method on a Date
object. Here is an example code snippet:
// Create a new Date object for the current date
const currentDate = new Date();
// Get the name of the month using toLocaleString() method
const monthName = currentDate.toLocaleString('default', { month: 'long' });
console.log(monthName); // Output: February
In this example, we first create a new Date
object for the current date using the Date()
constructor without any arguments, which creates a Date
object for the current date and time.
We then use the toLocaleString()
method on the currentDate
object and pass in two arguments to format the output. The first argument is the string “default”, which tells the method to use the user’s default locale. The second argument is an object with a month
property set to the string “long”, which tells the method to return the full name of the month.
The toLocaleString()
method returns a string representing the currentDate
object in a human-readable format, with the month name extracted from the date. In this example, the output will be the name of the current month, which is “February”.