JavaScript getUTCDate(): Get the UTC Day of the Month (1-31)

The JavaScript getUTCDate() method is used to get the day of the month (1-31) 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.getUTCDate();
      document.getElementById("xyz").innerHTML = day;
   </script>

</body>
</html>
Output

In the above example, the following JavaScript statement:

const d = new Date();

creates a new "Date" object with the "new Date()" constructor. This method generates a new date and time object using the current system clock time.

Then the following JavaScript statement:

let day = d.getUTCDate();

calls the Date object's getUTCDate() method to obtain the current date and time's month day (in UTC). The result of getUTCDate() is saved in a variable called day.

Finally, using the following JavaScript statement:

document.getElementById("xyz").innerHTML = day;

The value of "day" is displayed in the "p" element with the id "xyz" by using the document's innerHTML property.Statement getElementById("xyz").

JavaScript getUTCDate() syntax

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

x.getUTCDate()

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

The getUTCDate() method returns a number from 1 to 31, which will be the day of the month based on the UTC time.

Print the Day of the Month based on UTC in JavaScript

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p>Today's Date: <span id="res"></span></p>

   <script>
      const d = new Date();
      document.getElementById("res").innerHTML = d.getUTCDate();
   </script>

</body>
</html>
Output

Today's Date:

This code displays the current date by retrieving the day of the month in UTC time from a Date object using the getUTCDate() method and setting the value of a span element with the id "res" to that day of the month.

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!