• JavaScript Video Tutorials

JavaScript Date getUTCDay() Method



The JavaScript Date.getUTCDay() method is used to retrieve the "day of the week" for a date according to Coordinated Universal Time (UTC). 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. Additionally, this method does not accept any parameters.

This method does returns Not a Number (NaN), if the provided date is invalid date.

Syntax

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

getUTCDay();

This method does not accept any parameters.

Return Value

An integer representing the day of the week in UTC time zone.

Example 1

Following is the basic example of JavaScript Date getUTCDay() method −

<html>
<body>
<script>
   const currentDate = new Date();
   const dayOfWeek = currentDate.getUTCDay();

   document.write("Current day of the week: ", dayOfWeek);
</script>
</body>
</html>

Output

It returns the "day of week" according to UTC.

Example 2

In the following example, we are providing a specific date to the date constructor −

<html>
<body>
<script>
   const specificDate = new Date("2023-12-20");
   const dayOfWeek = specificDate.getUTCDay();

   document.write("Day of the week for December 20, 2023: ", dayOfWeek);
</script>
</body>
</html>

Output

The above program retrieves "3" as the day of the week.

Example 3

This example uses a function, getDayName(), to get the day name based on the day index. It then displays the current day of the week in UTC using this function.

<html>
<body>
<script>

   function getDayName(utcDay) {
      const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
      return daysOfWeek[utcDay];
   }

   const currentDate = new Date();
   const dayOfWeek = currentDate.getUTCDay();
   const dayName = getDayName(dayOfWeek);

   document.write("Today is: ", dayName);
</script>
</body>
</html>

Output

It returns the current day of week according to UTC.

Advertisements