Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Selected Reading

JavaScript Date getDay() Method



The JavaScript Date.getDay() method is used to retrieve the "day of the week" (in local time) for a specified date object. This method does not accept any parameters. The returned value will represent the day of the week as an integer value between 0 to 6, where 0 represents sunday, 1 to monday, 2 to tuesday,..., and 6 to saturday.

This method returns the day of the week based on local time, not UTC time. If you need UTC-based day of the week, use getUTCDay() method.

Syntax

Following is the syntax of JavaScript Date getDay() method −

getDay();

This method does not accept any parameters.

Return Value

This method returns an integer representing the day of the week.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getDay() method −

<html>
<body>
<script>
   const CurrentDate = new Date();
   const DayOfWeek = CurrentDate.getDay();
   document.write(DayOfWeek);
</script>
</body>
</html>

Output

The above program returns the day of the week.

Example 2

Here, we are retrieving day of the week for a specific date "December 23, 2023" −

<html>
<body>
<script>
   const SpecificDate = new Date('2023-12-23');
   const DayOfWeek = SpecificDate.getDay();
   document.write(DayOfWeek);
</script>
</body>
</html>

Output

The above programs returns integer "6" as the day of the week for given date.

Example 3

If the current day of the week is 0 (sunday) or 6 (saturday), this program prints "It's the weekend"; else, it prints "It's a weekday" −

<html>
<body>
<script>
function isWeekend() {
   const currentDate = new Date('2023-12-23');
   const dayOfWeek = currentDate.getDay();

   if (dayOfWeek === 0 || dayOfWeek === 6) {
      document.write("It's the weekend");
   } else {
      document.write("It's a weekday.");
   }
}
isWeekend();
</script>
</body>
</html>

Output

The above program prints "It's the weekend" because the day of the week for the specified date is 6.

Advertisements