JavaScript Date getHours() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Hour (0–23)

What You’ll Learn

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

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.

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.

📝 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

⚡ Quick Reference

GoalCode
Current hournew Date().getHours()
Hour from fixed timenew Date(2026, 6, 4, 14, 30).getHours() → 14
UTC hourdate.getUTCHours()
Minutes componentdate.getMinutes()
Set hourdate.setHours(15)
Valid date check!Number.isNaN(date.getTime())

📋 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

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);
Try It Yourself

How It Works

new Date() captures “now.” getHours() returns only the hour integer in local 24-hour time—minutes and seconds are ignored for this getter.

📈 Practical Patterns

Fixed times, validation, greetings, and scheduled tasks.

Example 2 — Read the Hour from a Specific Time

Build a date at 2:30 PM local time and read its hour.

JavaScript
const afternoon = new Date(2026, 6, 4, 14, 30);

console.log(afternoon.getHours());    // 14
console.log(afternoon.getMinutes());  // 30
Try It Yourself

How It Works

The constructor new Date(y, m, d, h, min) uses local time. Hour 14 means 2:00 PM in 24-hour format.

Example 3 — Validate Before Calling getHours()

Guard against invalid Date objects that yield NaN.

JavaScript
function safeGetHours(date) {
  if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
    return null;
  }
  return date.getHours();
}

console.log(safeGetHours(new Date()));              // e.g. 7
console.log(safeGetHours(new Date("not-a-date")));  // null
Try It Yourself

How It Works

getTime() returns NaN on invalid dates. Checking first prevents broken comparisons like hour < 12 when hour is NaN.

Example 4 — Time-of-Day Greeting

Pick a message based on whether it is morning, afternoon, or evening.

JavaScript
function getGreeting() {
  const hour = new Date().getHours();

  if (hour < 12) {
    return "Good morning!";
  }
  if (hour < 18) {
    return "Good afternoon!";
  }
  return "Good evening!";
}

console.log(getGreeting());
Try It Yourself

How It Works

Boundaries are inclusive at the lower end: hours 0–11 are morning, 12–17 afternoon, 18–23 evening. Adjust thresholds to match your product copy.

Example 5 — Run a Task at a Scheduled Hour

Compare the current hour to a target hour (3 PM = 15).

JavaScript
const scheduledHour = 15; // 3:00 PM
const currentHour = new Date().getHours();

if (currentHour === scheduledHour) {
  console.log("Task scheduled for 3:00 PM is running.");
} else {
  console.log("No task at this hour.");
}
Try It Yourself

How It Works

This pattern checks only the hour—not minutes. For exact times, also compare getMinutes() or compare timestamps with getTime().

🚀 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.

📝 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.

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 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 support
Date.getHours() Excellent

Bottom line: Safe everywhere. Watch timezone and DST boundaries when pairing with UTC getters or server timestamps.

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.

💡 Best Practices

✅ Do

  • Validate dates before calling getHours()
  • Think in 24-hour values (15 = 3 PM)
  • Pick UTC getters when storing UTC instants
  • Combine with getMinutes() for exact times
  • Use locale formatters for user-facing clocks

❌ Don’t

  • Assume getHours() returns 1–12 AM/PM
  • Ignore NaN from bad parses
  • Mix UTC ISO strings with local getters blindly
  • Schedule to the minute using hour checks alone
  • Forget DST when testing edge hours

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.getHours()

Your foundation for hour reads in JavaScript.

5
Core concepts
🔢 02

0–23

24-hour clock.

Range
🌐 03

Local

Not UTC.

Timezone
👋 04

Greeting

Time-of-day UI.

Pattern
05

Not AM/PM

15 = 3 PM.

Pitfall

❓ Frequently Asked Questions

An integer from 0 to 23 representing the hour in the Date object's local timezone. It uses 24-hour time: midnight is 0, noon is 12, and 11 PM is 23.
getHours() uses local timezone rules. getUTCHours() reads the UTC hour. They can differ when your offset shifts the calendar day or hour.
No. It always returns 24-hour format (0–23). Convert to 12-hour display yourself or use toLocaleTimeString() for locale-aware formatting.
NaN. Validate with !Number.isNaN(date.getTime()) before using the result in conditionals 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';
Did you know?

Hour 0 is midnight and hour 12 is noon—both are valid getHours() results. If you see NaN, the Date object is invalid, not the hour value.

Continue to getMinutes()

Learn how to read the minute component (0–59) from a Date object in local time.

getMinutes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful