Home >>JavaScript Date Object Methods >JavaScript getHours() Method

JavaScript getHours() Method

JavaScript getHours() Method

JavaScript getHours() method is used to return the hour of the given input date and time. It returns an integer value between 0 to 23.

Syntax:
Date.getHours()

Browser Support

Method Chrome Edge Firefox Safari Opera
getHours() Yes Yes Yes Yes Yes
Here is an example of JavaScript getHours() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() 
{
  var d = new Date();
  var n = d.getHours();
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() 
{
  var d = new Date("June 10, 1950 13:30:00");
  var n = d.getHours();
  document.getElementById("demo1").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.


No Sidebar ads