JavaScript Date getHours() Method

Definition
Date.prototype.getHours() returns the hour of the day as an integer 0–23 in local time (24-hour clock). It is read-only and does not change the Date.
date.getHours()
0 = midnight
12 = noon
23 = 11 PM
→ integer 0 … 23
→ NaN if the Date is invalid
getHours() → local hour
getUTCHours() → UTC hour
Syntax
Call date.getHours. Start with this line.
| Call | What it does |
|---|
date.getHours() | Primary signature. |
date.getHours(...) | Call with the arguments your case needs. |
Built into JavaScript. Call getHours(...) on a Date instance — no import required.
Library declarations
Parameters
This method has no parameters beyond those shown in the signature.
Return Value
| Return Value | Description |
|---|
| * | The value returned by date.getHours(). |
JavaScript Examples
Example 1 — Read the Local Hour
Call getHours() on a fixed local date-time (like the MDN pattern).
const morning = new Date(2008, 2, 13, 4, 20); // Mar 13, 2008 04:20
const night = new Date(1995, 11, 25, 23, 15, 30);
console.log(morning.getHours()); // 4
console.log(night.getHours()); // 23
How It Works
- Build local times. Month indexes are zero-based (
2 = March, 11 = December). - Early morning.
04:20 → getHours() returns 4. - Late evening.
23:15 → 23 on the 24-hour clock. - No mutation. The
Date values are unchanged.
Example 2 — getHours() vs getUTCHours()
Local and UTC hours can disagree when an offset shifts the clock hour.
const d = new Date("1995-12-25T23:15:30Z"); // 23:15 UTC
console.log(d.getUTCHours()); // 23
console.log(d.getHours()); // local hour (often not 23)
23
(local hour — depends on your timezone)
How It Works
- UTC instant. The
Z suffix means 23:15 in UTC. - UTC getter.
getUTCHours() always reports 23 for that instant. - Local getter.
getHours() applies your timezone offset — result varies by machine. - Rule. Need wall-clock local time →
getHours(); need UTC → getUTCHours().
Example 3 — Time-of-Day Greeting
Branch on the hour for a simple morning / afternoon / evening message.
function greetingFor(date) {
const h = date.getHours();
if (h < 12) return "Good morning";
if (h < 18) return "Good afternoon";
return "Good evening";
}
console.log(greetingFor(new Date(2026, 0, 1, 9, 0))); // Good morning
console.log(greetingFor(new Date(2026, 0, 1, 15, 0))); // Good afternoon
console.log(greetingFor(new Date(2026, 0, 1, 20, 0))); // Good evening
Good morning
Good afternoon
Good evening
How It Works
- Read the hour.
getHours() gives a 0–23 value for branching. - Morning. Hours
0–11 map to “Good morning.” - Afternoon. Hours
12–17 map to “Good afternoon.” - Evening. Hours
18–23 map to “Good evening.”
Compatibility
Browser & Runtime Support
Date.prototype.getHours() is part of the language from early ECMAScript and is Baseline Widely available (MDN).
✓ Baseline · Widely available
Date.prototype.getHours()
Supported in every modern browser and Node.js. No polyfill needed for ordinary web apps.
99% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full supportDate.getHours() Excellent
Bottom line: Safe everywhere. Focus on timezone choice (local vs UTC) and invalid-Date checks rather than feature detection.
Frequently Asked Questions
An integer from 0 to 23 for the hour in local time. Midnight is 0, noon is 12, and 11 PM is 23. Invalid Dates return NaN.
getHours() uses the local timezone. getUTCHours() uses UTC. They can differ when your offset moves the clock hour across a day boundary.
No. It always returns 24-hour format (0–23). Convert yourself, or use toLocaleTimeString() for locale-aware display.
NaN. Validate with Number.isNaN(date.getTime()) before using the result in conditions or UI.
Midnight is 0. There is no hour 24 — 23 is the last hour of the day.
const h = new Date().getHours(); if (h < 12) greeting = 'Good morning'; else if (h < 18) greeting = 'Good afternoon'; else greeting = 'Good evening';
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful