getHours() returns the hour of the day as an integer from 0 to 23 in local time (24-hour clock). This guide covers syntax, five examples, UTC comparisons, validation, and common patterns like greetings and schedulers.
01
Syntax
date.getHours()
02
Range
0 – 23
03
24-hour
Not AM/PM
04
Local time
Not UTC
05
Greetings
Time-of-day UI
06
Validate
Invalid → NaN
Fundamentals
Introduction
Dashboards, chat apps, and login screens often change behavior by time of day. The getHours() method reads the hour component from a Date object using the user’s local timezone.
Combine it with getMinutes() and getSeconds() for precise clocks, or use toLocaleTimeString() when you need a formatted display string.
Concept
Understanding the getHours() Method
Date.prototype.getHours() is a zero-argument instance method. It never mutates the original Date—it only reads the hour in 24-hour local time.
Midnight returns 0, noon returns 12, and 11 PM returns 23. If you need a 12-hour clock with AM/PM, convert the value or use locale formatters.
💡
Beginner Tip
3:00 PM is hour 15, not 3. Add 12 to PM hours (except noon) when converting from a 12-hour clock mentally.
Foundation
📝 Syntax
JavaScript
dateObj.getHours()
Parameters
None.
Return value
Integer 0–23 for a valid date in local time.
NaN if dateObj is an invalid Date.
Sample values
0 — midnight (12:00 AM)
9 — 9:00 AM
12 — noon
15 — 3:00 PM
23 — 11:00 PM
Cheat Sheet
⚡ Quick Reference
Goal
Code
Current hour
new Date().getHours()
Hour from fixed time
new Date(2026, 6, 4, 14, 30).getHours() → 14
UTC hour
date.getUTCHours()
Minutes component
date.getMinutes()
Set hour
date.setHours(15)
Valid date check
!Number.isNaN(date.getTime())
Compare
📋 getHours() vs Similar Methods
These getters split the same instant into different time parts. Match the method to what your UI or rule needs.
getHours()
0–23
Local hour
getUTCHours()
UTC hour
Timezone-safe read
getMinutes()
0–59
Minute part
toLocaleTimeString
"2:30 PM"
Formatted string
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Fixed times use new Date(y, m, d, h, min) for predictable local values.
📚 Getting Started
Read the hour from the current date and time.
Example 1 — Get the Current Hour
Call getHours() on a new Date() to read the hour right now.
JavaScript
const now = new Date();
const currentHour = now.getHours();
console.log("Current hour:", currentHour);
This pattern checks only the hour—not minutes. For exact times, also compare getMinutes() or compare timestamps with getTime().
Applications
🚀 Common Use Cases
Dashboard greetings — morning vs evening welcome text.
Business hours — show “we’re open” between 9 and 17.
Dark mode — auto-switch theme after a set hour.
Hourly jobs — trigger cleanup when getHours() === 0.
Analytics — bucket events by hour of day.
Rate limits — reset counters at midnight local time.
🧠 How getHours() Resolves the Hour
1
Internal instant
The Date stores milliseconds since Unix epoch (UTC).
Storage
2
Local conversion
Engine applies the runtime timezone offset.
TZ
3
Extract hour
Hour 0–23 is returned in 24-hour local time.
getHours
4
🕐
Use in logic
Compare, greet, schedule, or pass to setHours.
Important
📝 Notes
getHours() is local; use getUTCHours() when you standardize on UTC.
Returns 24-hour time (0–23), not 12-hour AM/PM.
Invalid dates propagate NaN—validate first.
Hour-only checks ignore minutes—combine getters for exact times.
Daylight saving shifts can produce skipped or repeated local hours.
For display, prefer toLocaleTimeString() or Intl.DateTimeFormat.
Compatibility
Browser & Runtime Support
Date.prototype.getHours() has been available since the first JavaScript Date implementation (ES1). It works in every browser and Node.js.
✓ Baseline · ES1
Date.prototype.getHours()
Supported in Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions. No polyfill required.
99%Universal API
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Date.getHours()Excellent
Bottom line: Safe everywhere. Watch timezone and DST boundaries when pairing with UTC getters or server timestamps.
Wrap Up
Conclusion
getHours() is the standard way to read the local hour (0–23) from a JavaScript Date. Use it for time-of-day logic, validate invalid dates, and pair with minute/second getters when precision matters.
Next, learn getMinutes() for the minute component, or compare with getUTCHours() for UTC-based rules.