JavaScript getUTCDay(): Get UTC Day of the Week

The JavaScript getUTCDay() method is used to get the day of the week (0-6) according to Universal Time Coordinated (UTC) or Greenwich Mean Time (GMT). For example:

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

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

</body>
</html>
Output

JavaScript getUTCDay() syntax

The syntax of the getUTCDay() method in JavaScript is:

x.getUTCDay()

where x must be an object of the Date() constructor.

The getUTCDay() method returns a number from 0 to 6, which indicates the day of the week.

Find the name of the UTC weekday in JavaScript

The following code finds and outputs the name of the UTC weekday using 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.getUTCDay();

         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 example can also be written as:

<!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.getUTCDay()];
      </script>

</body>
</html>

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

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

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!