Weekday as a number in JavaScript?


In this article, we are going learn about the weekday as a number in JavaScript with appropriate examples.

To get the weekday as a number, JavaScript has provided getDay() method. The getDay() is a method from Date object. The getDay() method returns a value between 0 to 6. For example, Sunday = 0, Monday =1, Tuesday =2, Wednesday = 3 and so on.

To get a better understanding, let’s look into the syntax and usage of getDay() method in JavaScript.

Syntax

The syntax for getDay() method is −

dateObject.getDay();

Where, dateObject is an object created from date. This method returns a number between 0 to 6 (0 – Sunday, 1 – Monday, 2 – Tuesday, 3 – Wednesday, 4 – Thursday, 5 – Friday, 6 – Saturday).

Example 1

This is an example program to get the weekday of the current date as a number in JavaScript using getDay() method.

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      const date = new Date(); //Get the day of the week of current date
      const day = date.getDay();
      document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

This is an example program to get the weekday of the user-mentioned date as a number in JavaScript using getDay() mehod.

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      const date = new Date("January 4 2001 04:04:04"); //Get the day of the week of particular date mentioned by the user
      const day = date.getDay();
      document.getElementById("output").innerHTML = 'Week day as a number for the date : ' + date + ' is : ' + day;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

This is an example program to define a user defined function that is based on Zeller’s congruence to get weekday as a number in JavaScript.

<!DOCTYPE html>
<html>
<head>
   <title>Weekday as a number in JavaScript</title>
</head>
<body style="text-align : center">
   <h3>Weekday as a number in JavaScript</h3>
   <p>The returned value is an integer, which is the day of the week (0 to 6).
      i.e. 0 - Sunday
      1 - Monday
      ....
      6 - Saturday .
   </p>
   <p id="output"></p>
   <script>
      // Without using Date() and getDay()
      var dd,mm,yyyy;
      function calculateDay(dd, mm, yyyy) {
         const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
         const indexes_of_weekday = [0,1,2,3,4,5,6];
         if (mm< 3) {
            mm += 12;
            yyyy--;
         }
         var h = (dd + parseInt(((mm + 1) * 26) / 10) + yyyy + parseInt(yyyy / 4) + 6 * parseInt(yyyy / 100) + parseInt(yyyy / 400) - 1) % 7;
         return indexes_of_weekday[h];
      }
      var day = calculateDay(23,06,2022);
      document.getElementById("output").innerHTML = 'The day is : ' + day;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 09-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements