JavaScript date.getDay() method


The date.getDay() method in JavaScript returns an integer representing the day of the week for a specific Date object, according to local time. The value ranges from 0 to 6, where:

  • 0 is Sunday
  • 1 is Monday
  • 2 is Tuesday
  • 3 is Wednesday
  • 4 is Thursday
  • 5 is Friday
  • 6 is Saturday

Syntax:

date.getDay();

Returns:

  • An integer from 0 (Sunday) to 6 (Saturday), representing the day of the week.

Example 1: Getting the Day of the Week for a Specific Date

const date = new Date('2024-10-22'); // October 22, 2024 const dayOfWeek = date.getDay(); console.log(dayOfWeek);

Output:

2

Explanation:

  • The date "2024-10-22" is a Tuesday, so getDay() returns 2, representing Tuesday.

Example 2: Getting the Current Day of the Week

const today = new Date(); // Current date const currentDay = today.getDay(); console.log(currentDay);

Output:

(Depends on the current day of the week)

Explanation:

  • If today is, for example, Friday, the output will be 5. This retrieves the current day of the week according to the system's local time zone.

Example 3: Getting the Day of the Week for a Different Date

const date = new Date('2022-07-04'); // July 4, 2022 const dayOfWeek = date.getDay(); console.log(dayOfWeek); // Output will be 1 (Monday)

Output:

1

Explanation:

  • July 4, 2022, was a Monday, so getDay() returns 1.

Summary:

  • date.getDay() is used to get the day of the week as a number (0-6) for a given date.
  • Sunday is represented by 0, and Saturday by 6.