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 Sunday1
is Monday2
is Tuesday3
is Wednesday4
is Thursday5
is Friday6
is Saturday
Syntax:
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
Output:
Explanation:
- The date
"2024-10-22"
is a Tuesday, sogetDay()
returns2
, representing Tuesday.
Example 2: Getting the Current Day of the Week
Output:
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
Output:
Explanation:
- July 4, 2022, was a Monday, so
getDay()
returns1
.
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 by6
.