JavaScript Date getHours()
Examples
Get the hours:
const d = new Date();
let hour = d.getHours();
Try it Yourself »
Get the hours from a specific date:
const d = new Date("July 21, 1983 01:15:00");
let hour = d.getHours();
Try it Yourself »
More examples below.
Definition and Usage
getHours()
returns the hour (0 to 23) of a date.
Syntax
Date.getHours()
Parameters
NONE |
Return Value
Type | Description |
A number | The local time hour (0 to 23). |
Browser Support
getHours()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
More Examples
Add zeros and colons to display the time:
function addZero(i) {
if (i < 10) {i = "0" + i}
return i;
}
const d = new Date();
let h = addZero(d.getHours());
let m = addZero(d.getMinutes());
let s = addZero(d.getSeconds());
let time = h + ":" + m + ":" + s;
Try it Yourself »