JavaScript Date getHours() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

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.

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

JavaScript
date.getHours()
CallWhat 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
JavaScript
date.getHours()

Parameters

This method has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*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).

JavaScript
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
Try It Yourself

How It Works

  1. Build local times. Month indexes are zero-based (2 = March, 11 = December).
  2. Early morning. 04:20 → getHours() returns 4.
  3. Late evening. 23:15 → 23 on the 24-hour clock.
  4. 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.

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

How It Works

  1. UTC instant. The Z suffix means 23:15 in UTC.
  2. UTC getter. getUTCHours() always reports 23 for that instant.
  3. Local getter. getHours() applies your timezone offset — result varies by machine.
  4. 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.

JavaScript
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
Try It Yourself

How It Works

  1. Read the hour. getHours() gives a 0–23 value for branching.
  2. Morning. Hours 0–11 map to “Good morning.”
  3. Afternoon. Hours 12–17 map to “Good afternoon.”
  4. Evening. Hours 18–23 map to “Good evening.”

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 support
Date.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';

Next: getMinutes()

Learn how to read the local minutes field (0–59) from a Date.

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.

11 people found this page helpful