The getUTCHours method in JavaScript retrieves the hour component of the given date in accordance with Coordinated Universal Time (UTC).
Syntax
The method getUTCHours is denoted by the syntax below:
Example
dateObj.getUTCHours()
Return
An integer ranging from 0 to 23 that signifies the hour of the day.
JavaScript Date getUTCHours method example
In this section, we will explore the getUTCHours method by examining several examples.
Example 1
Let’s examine an example that displays the current hour based on Coordinated Universal Time (UTC).
Example
<script>
var hour=new Date();
document.writeln("Hour value : "+hour.getUTCHours());
</script>
Output:
Output
Hour value : 12
Example 2
Let’s examine an example that demonstrates how to extract and display the hour from a specified date in accordance with Coordinated Universal Time (UTC).
Example
<script>
var hour1=new Date("August 15 1975 22:15:30 GMT+11:00");
var hour2=new Date("August 15 1975 22:15:30 GMT-11:00");
document.writeln("Hour value1 : "+hour1.getUTCHours()+"<br>");
document.writeln("Hour value2 : "+hour2.getUTCHours());
</script>
Output:
Output
Hour value1 : 11
Hour value2 : 9