There are several ways to get the day of the week for a specific date in JavaScript. Here are three common methods:
Date
object:var date = new Date('2022-09-10');
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dayOfWeek = days[date.getDay()];
console.log(dayOfWeek); // Output: Saturday
toLocaleDateString
method:var date = new Date('2022-09-10');
var options = { weekday: 'long' };
var dayOfWeek = date.toLocaleDateString('en-US', options);
console.log(dayOfWeek); // Output: Saturday
var date = moment('2022-09-10');
var dayOfWeek = date.format('dddd');
console.log(dayOfWeek); // Output: Saturday
All these methods work similarly by first creating a Date
object with the specific date and then retrieving the day of the week from it.