How to get the day of the week for a specific date in JavaScript?

There are several ways to get the day of the week for a specific date in JavaScript. Here are three common methods:

  1. Using the 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
  1. Using the 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
  1. Using a library like Moment.js (if it's available and you prefer using external libraries):
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.