Date.prototype.getHours
Returns the hour.
Available since: ES1
Syntax
date.getHours()Return value
numberThe hour from 0 to 23 in local time.
Examples
The hour of a date:
console.log(new Date(2026, 2, 15, 18, 45).getHours());Output:
18A greeting by time of day:
const hour = new Date(2026, 2, 15, 9, 0).getHours();
console.log(hour < 12 ? 'Good morning' : 'Good afternoon');Output:
Good morningNotes
The value depends on the computer's time zone. The UTC hour comes from getUTCHours().
See also
- Date.prototype.getMinutes — Returns the minutes.
- Date.prototype.setHours — Sets the hour, and optionally minutes, seconds and milliseconds.
In the tutorial
- Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.