The getDay()
method in JavaScript is used to get the day of the week for the specified date according to local time. The day of the week is represented as an integer, with 0 representing Sunday, 1 representing Monday, and so on.
Here is an example of how to use the getDay()
method:
const date = new Date();
const dayOfWeek = date.getDay();
console.log(dayOfWeek); // output: 6 (assuming today is Saturday)
You can also pass a date as an argument to the Date
constructor to get the day of the week for a specific date:
const date = new Date('January 1, 2021');
const dayOfWeek = date.getDay();
console.log(dayOfWeek); // output: 5 (Friday)
Note that the getDay()
method returns the day of the week in the local timezone. If you want to get the day of the week in a specific timezone, you can use the getUTCDay()
method instead.
For example:
const date = new Date();
const utcDayOfWeek = date.getUTCDay();
console.log(utcDayOfWeek); // output: 6
Leave a Reply