Example
Return the hour, according to local time:
var d = new Date();
var n = d.getHours();
The result of n will be:
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The getHours() method returns the hour (from 0 to 23) of the specified date and time.
Browser Support
Method | |||||
---|---|---|---|---|---|
getHours() | Yes | Yes | Yes | Yes | Yes |
Syntax
Date.getHours()
Parameters
None |
Technical Details
Return Value: | A Number, from 0 to 23, representing the hour |
---|---|
JavaScript Version: | 1.0 |
More Examples
Example
Return the hour from a specific date and time:
var d = new Date("July 21, 1983 01:15:00");
var n = d.getHours();
The result of n will be:
1
Try it Yourself »
Example
Using getHours(), getMinutes(), and getSeconds() to display the time:
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function myFunction() {
var d = new Date();
var x = document.getElementById("demo");
var h =
addZero(d.getHours());
var m =
addZero(d.getMinutes());
var s =
addZero(d.getSeconds());
x.innerHTML = h + ":" + m +
":" + s;
}
Try it Yourself »
< JavaScript Date Object