JavaScript getDay() | Get Day of the Week

The JavaScript getDay() method is used when we need to find the day of the week. This method returns a number from 0 to 6 where 0 indicates to Sunday and 6 indicates to Saturday. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
    
    <p id="xyz"></p>

    <script>
        const d = new Date();
        let day = d.getDay();
        document.getElementById("xyz").innerHTML = day;
    </script>

</body>
</html>
Output

JavaScript getDay() Syntax

The syntax of getDay() method in JavaScript is:

x.getDay()

The x refers to an object of the Date() constructor.

The getDay() method returns a number from 0 to 6 where:

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

Find Name of the Weekday in JavaScript

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
      <p>Today is <span id="res"></span></p>

      <script>
         const d = new Date();
         let day = d.getDay();

         if(day==0)
            document.getElementById("res").innerHTML = "Sunday";
         else if(day==1)
            document.getElementById("res").innerHTML = "Monday";
         else if(day==2)
            document.getElementById("res").innerHTML = "Tuesday";
         else if(day==3)
            document.getElementById("res").innerHTML = "Wednesday";
         else if(day==4)
            document.getElementById("res").innerHTML = "Thursday";
         else if(day==5)
            document.getElementById("res").innerHTML = "Friday";
         else if(day==6)
            document.getElementById("res").innerHTML = "Saturday";
      </script>

</body>
</html>
Output

Today is

The same JavaScript example can also be written as:

<!DOCTYPE html>
<html>
<body>
   
      <p>Today is <span id="res"></span></p>

      <script>
         const d = new Date();
         let day = d.getDay();
         let weekday;

         if(day==0)
            weekday = "Sunday";
         else if(day==1)
            weekday = "Monday";
         else if(day==2)
            weekday = "Tuesday";
         else if(day==3)
            weekday = "Wednesday";
         else if(day==4)
            weekday = "Thursday";
         else if(day==5)
            weekday = "Friday";
         else if(day==6)
            weekday = "Saturday";

         document.getElementById("res").innerHTML = weekday;
      </script>

</body>
</html>

Or. In following example, I am going to use an array where I will initialize all the seven weekday names. Then will access names based on their indexes.

<!DOCTYPE html>
<html>
<body>
   
      <p>Today is <span id="res"></span></p>

      <script>
         const d = new Date();
         let day = d.getDay();
         const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"];
         document.getElementById("res").innerHTML = weekday[day];
      </script>

</body>
</html>

Or

<!DOCTYPE html>
<html>
<body>
   
      <p>Today is <span id="res"></span></p>

      <script>
         const d = new Date();
         const weekday = ["Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"];
         document.getElementById("res").innerHTML = weekday[d.getDay()];
      </script>

</body>
</html>

To get or print only three letters (first three letters) of the weekday, then replace the weekday array from the above (previous) example with:

const weekday = ["Sun", "Mon", "Tue", "Wed",
   "Thu", "Fri", "Sat"];

Please note: To display date in format dd-mm-yyyy, refer to its separate example.

Please note: To display time in format hh:mm:ss, refer to its separate example.

Please note: To display time in format hh:mm:ss AM/PM, refer to its separate example.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋