The getDay method in JavaScript retrieves the numerical representation of the day of the week for a given date, taking into account the local time. The days are indexed starting from 0, where 0 corresponds to Sunday.
Syntax
The syntax for the getDay method is as follows:
Example
dateObj.getDay()
Return
A numerical value ranging from 0 to 6 that indicates the days of the week corresponding to the given date.
JavaScript Date getDay method example
In this section, we will explore the getDay method using a variety of examples.
Example 1
Let’s examine an illustration that demonstrates how to display the current day's weekday value.
Example
<script>
var day=new Date();
document.writeln(day.getDay());
</script>
Output:
Example 2
Let’s examine an example that demonstrates how to output the day of the week based on a specified date.
Example
<script>
var day=new Date("August 15, 1947 20:22:10");
document.writeln(day.getDay())
</script>
Output: