Date.prototype.getHours()
Return the hour of a date in 24-hour form according to local time.
getHours()- Added in JavaScript ES1
Return value
numberAn integer between 0 and 23 representing the hours for the given date according to local time. Returns NaN if the date is invalid.
Errors
Throws a TypeError if called on a value that is not a Date object.
Returns the hour in 24-hour form, 0 through 23, in the timezone the code is running in. Midnight is 0 and 11pm is 23; there is no 24.
When to reach for it
Use it for anything keyed to the time of day: a greeting that changes with the hour, opening-hours checks, bucketing events into hourly slots for a chart, or formatting a clock by hand alongside getMinutes() and getSeconds().
What trips people up
There is no 12-hour variant. Converting is hours % 12 || 12 — the || 12 matters, because 0 % 12 is 0 and midnight would otherwise print as "0am". The meridiem is hours < 12 ? "am" : "pm", so noon is 12pm and midnight is 12am.
The value is local, which makes daylight saving visible. On a spring-forward day one local hour simply does not exist: setting 02:30 gives you 03:30, and adding 3,600,000 ms to 01:30 lands on 03:30, so the hour appears to jump by two. On the autumn transition an hour repeats, and two distinct instants report the same hour. Any code that assumes consecutive hours differ by exactly one will break twice a year — use getUTCHours(), or work in timestamps, when that matters.
Example
When to use it
- Show a greeting that changes between morning, afternoon and evening
- Check whether the current time falls inside opening hours
- Bucket log entries into 24 hourly slots for a histogram
- Format a timestamp as HH:MM by hand
Frequently asked questions
How do I convert getHours() to a 12-hour clock?
Use hours % 12 || 12 for the number and hours < 12 ? "am" : "pm" for the suffix. The || 12 is required because 0 % 12 is 0, which would print midnight as 0am.
Does getHours() account for daylight saving time?
Yes — it reports the local wall-clock hour, so on transition days one hour is skipped and another repeats. Use getUTCHours() or compare raw timestamps when you need hours that always advance by one.
Why does getHours() return 0 instead of 24 at midnight?
The range is 0 to 23, so midnight is hour 0 and there is no hour 24. The last hour of a day is 23, which covers 23:00 to 23:59.
More examples
Time-of-day greeting
The boundaries are exclusive upper bounds, so hour 12 is the first afternoon hour. Building the Date with an explicit hour keeps the output the same on every machine.
function greet(date) {
const h = date.getHours();
if (h < 5) return "Still up?";
if (h < 12) return "Good morning";
if (h < 18) return "Good afternoon";
return "Good evening";
}
console.log([2, 6, 11, 13, 19, 23]
.map((h) => greet(new Date(2024, 0, 1, h))));
console.log(greet(new Date(2024, 0, 1, 12)));Opening hours, including overnight
A 9-to-18 window is a simple range test, but a 20-to-04 window wraps past midnight and needs the OR branch. Without it, every late-night hour reads as closed.
function isOpen(date, open, close) {
const h = date.getHours();
return open <= close ? h >= open && h < close // same-day window
: h >= open || h < close; // wraps past midnight
}
const at = (h) => new Date(2024, 0, 1, h);
console.log([8, 9, 17, 18].map((h) => isOpen(at(h), 9, 18)));
console.log([21, 23, 2, 6].map((h) => isOpen(at(h), 20, 4)));Hourly histogram
Because getHours() is already 0-23 it indexes the bucket array directly. Five distinct hours are populated, hour 9 holds three entries, and the total matches the input length.
const stamps = [1, 1, 3, 9, 9, 9, 14, 23].map((h) => new Date(2024, 0, 1, h, 30));
const buckets = new Array(24).fill(0);
for (const d of stamps) buckets[d.getHours()]++;
console.log(buckets.filter((n) => n > 0).length);
console.log(buckets[9], buckets[1], buckets[23]);
console.log(buckets.reduce((a, b) => a + b, 0));
Discussion