Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

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:

18

A 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 morning

Notes

The value depends on the computer's time zone. The UTC hour comes from getUTCHours().

See also

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.

Thanks for reading!

Donate to the site — we work hard for you.