The getHours method in JavaScript retrieves the hour component for a given date according to the local time zone.
Syntax
The syntax for the getHours method is as follows:
Example
dateObj.getHours()
Return
A whole number ranging from 0 to 23 that signifies the hour.
JavaScript Date getHours method example
In this section, we will explore the getHours function by examining several illustrative examples.
Example 1
Let’s examine an example that demonstrates how to display the current hour's value.
Example
<script>
var hour=new Date();
document.writeln(hour.getHours());
</script>
Output:
Example 2
Let’s examine an example that demonstrates how to extract and display the hour from a specified date.
Example
<script>
var hour=new Date("August 15, 1947 20:22:10");
document.writeln(hour.getHours());
</script>
Output: