JavaScript getUTCHours(): Get the UTC hour

The JavaScript getUTCHours() method is used to get the hour (0-23) 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 hour = d.getUTCHours();
      document.getElementById("xyz").innerHTML = hour;
   </script>

</body>
</html>
Output

In the above example, JavaScript's first line creates a new "Date" object and assigns it to the variable "d." This "Date" object represents the current date and time, as determined by the computer running the script's system clock.

The second line of JavaScript calls the "Date" object d's getUTCHours() method and assigns the result to the variable "hour." This method returns the current hour in the UTC time zone, which is the Coordinated Universal Time standard that all timekeeping systems around the world use.

The JavaScript code in the third line employs the getElementById() method to locate the HTML element with the id attribute "xyz," which is a paragraph element (p).

Finally, the JavaScript code in the third line again sets the paragraph element's innerHTML property to the value of the hour variable. This displays the current UTC hour in the web page's paragraph element.

JavaScript getUTCHours() Syntax

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

x.getUTCHours()

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

The getUTCHours() method returns a number from 0 to 23, which will be the hour of the UTC time.

Advantages of the getUTCHours() function in JavaScript

Disadvantages of the getUTCHours() function in JavaScript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!