You can get the name of the day 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 day using toLocaleString() method
const dayName = currentDate.toLocaleString('default', { weekday: 'long' });
console.log(dayName); // Output: Friday
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 weekday
property set to the string “long”, which tells the method to return the full name of the day of the week.
The toLocaleString()
method returns a string representing the currentDate
object in a human-readable format, with the day name extracted from the date. In this example, the output will be the name of the current day of the week, which is “Friday”.