JavaScript Date setHours() Method

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

What You’ll Learn

setHours() writes the hour (0–23) on a Date object in local time, with optional minutes, seconds, and milliseconds. This guide covers syntax, scheduling patterns, overflow, and comparisons with UTC setters.

01

Syntax

setHours(h)

02

24h

0 – 23

03

Overload

h, m, s, ms

04

Mutates

In place

05

+ hours

getHours + n

06

Returns

Epoch ms

Introduction

Reminders, meeting times, and “open at 9 AM” rules need to change the clock on a Date. After reading the hour with getHours(), setHours() writes it back in local time.

Like other setters, it mutates the original object and returns epoch milliseconds—not the Date for chaining.

Understanding the setHours() Method

Date.prototype.setHours(hour [, minute [, second [, millisecond]]]) accepts a 24-hour local hour and up to three optional time fields. When you pass only the hour, minutes, seconds, and milliseconds stay the same unless overflow normalization shifts the calendar day.

Values outside normal ranges roll forward or backward— setHours(25) becomes 1:xx on the next day. For UTC hour writes, use setUTCHours().

💡
Beginner Tip

To schedule at exactly 3:00 PM: date.setHours(15, 0, 0, 0). To add two hours: date.setHours(date.getHours() + 2).

📝 Syntax

JavaScript
dateObj.setHours(hour)
dateObj.setHours(hour, minute)
dateObj.setHours(hour, minute, second)
dateObj.setHours(hour, minute, second, millisecond)

Parameters

  • hour — Integer 0–23 in local 24-hour time (overflow normalizes).
  • minute (optional) — 0–59.
  • second (optional) — 0–59.
  • millisecond (optional) — 0–999.

Return value

  • Updated epoch milliseconds after the change.
  • NaN if the Date was invalid before the call.

⚡ Quick Reference

GoalCode
Set hour onlydate.setHours(8)
Snap to 15:00:00.000date.setHours(15, 0, 0, 0)
Add 2 hoursdate.setHours(date.getHours() + 2)
Read hour after setdate.getHours()
UTC hour setterdate.setUTCHours(15)
Clone before mutateconst copy = new Date(date)

📋 setHours() vs Similar Methods

Pick the setter that matches the time field and timezone you need.

setHours()
local 0–23

Local hour

getHours()
read

Getter pair

setUTCHours()
UTC 0–23

UTC setter

setMinutes()
minutes

Minute setter

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples use the Date constructor for predictable local times.

📚 Getting Started

Change only the hour while keeping minutes and seconds.

Example 1 — Set the Hour to 8 (Keep Minutes and Seconds)

Start at 12:30:45 and move the hour hand to 8.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45);
date.setHours(8);

console.log(date.getHours());    // 8
console.log(date.getMinutes());  // 30
console.log(date.getSeconds());  // 45
Try It Yourself

How It Works

Passing one argument updates only the hour. Minutes and seconds remain unless normalization rolls the day.

📈 Practical Patterns

Exact time snaps, relative hours, return values, and overflow.

Example 2 — Schedule a Meeting at 15:00:00.000

Use the four-argument overload to zero out sub-minute fields.

JavaScript
const meeting = new Date(2026, 2, 1, 9, 45, 30, 500);
meeting.setHours(15, 0, 0, 0);

console.log(meeting.getHours());         // 15
console.log(meeting.getMinutes());       // 0
console.log(meeting.getSeconds());       // 0
console.log(meeting.getMilliseconds());  // 0
Try It Yourself

How It Works

Hour 15 is 3 PM in 24-hour time. Setting minutes, seconds, and ms to 0 removes leftover sub-minute precision.

Example 3 — Add Two Hours with setHours(getHours() + 2)

Shift a deadline forward by two local hours.

JavaScript
const date = new Date(2026, 2, 1, 22, 30, 0);
date.setHours(date.getHours() + 2);

console.log(date.getHours());  // 0 (next day)
console.log(date.getDate());   // 2
Try It Yourself

How It Works

22:30 + 2 hours crosses midnight into 00:30 on the next calendar day. Overflow adjusts the date automatically.

Example 4 — Use the Return Value (Epoch Milliseconds)

setHours returns the updated timestamp—not the Date object.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45);
const ms = date.setHours(8);

console.log("Type:", typeof ms);                    // "number"
console.log("Matches getTime():", ms === date.getTime()); // true
Try It Yourself

How It Works

Call each setter on the same date variable. Do not chain .setMinutes() on the numeric return value.

Example 5 — Hour Overflow (setHours(25))

Passing 25 rolls into 1:xx on the next calendar day.

JavaScript
const date = new Date(2026, 2, 1, 12, 30, 45);
date.setHours(25);

console.log(date.getHours());  // 1
console.log(date.getDate());   // 2
Try It Yourself

How It Works

Overflow is normalized, not rejected. That is useful for adding hours but surprising if you expected clamping to 0–23.

🚀 Common Use Cases

  • Meeting reminders — snap events to setHours(15, 0, 0, 0).
  • Business hours — open at 9, close at 17 in local time.
  • Relative shifts — postpone by N hours with getHours() + N.
  • Form time pickers — apply user-selected hour to a Date.
  • Day boundaries — set midnight with setHours(0, 0, 0, 0).
  • Clone + mutate — copy a Date before changing hours for comparisons.

🧠 How setHours() Updates the Clock

1

Read local time

Engine loads current local date and time fields.

Local
2

Apply new hour

Hour and optional minute/second/ms replace old values.

setHours
3

Normalize overflow

Out-of-range values roll into adjacent hours or days.

Normalize
4

Mutate & return ms

Date updates in place; epoch milliseconds are returned.

📝 Notes

  • setHours() uses 24-hour local time (0 = midnight, 12 = noon).
  • Mutates the original Date—clone with new Date(date) when needed.
  • Returns epoch ms, not the Date—no fluent chaining on the return value.
  • Overflow normalizes (e.g. hour 25 → next day hour 1).
  • For real timezone conversion, use Intl or libraries—not naive getHours() + offset on UTC strings.
  • Use setUTCHours when rules follow UTC, not local wall clock.

Browser & Runtime Support

Date.prototype.setHours() has been available since ES1. It works in every browser and Node.js.

Baseline · ES1

Date.prototype.setHours()

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.setHours() Excellent

Bottom line: Safe everywhere. Remember 24-hour time, mutation, and the four-argument snap pattern.

Conclusion

setHours() writes the local hour (and optionally minutes, seconds, and milliseconds) on a JavaScript Date. Use it for scheduling and relative shifts; clone first when preserving the original instant.

Next, learn setMilliseconds() to change the sub-second fraction, or review getHours() for reading the hour.

💡 Best Practices

✅ Do

  • Use setHours(15, 0, 0, 0) for exact wall-clock times
  • Think in 24-hour values (15 = 3 PM)
  • Clone before mutating shared Date instances
  • Pair with getHours() for relative shifts
  • Use setUTCHours for UTC scheduling rules

❌ Don’t

  • Chain setters on the numeric return value
  • Assume 12-hour input without converting
  • Treat getHours() + offset as timezone conversion
  • Mix local setters with UTC getters in one label
  • Forget midnight is hour 0, not 24

Key Takeaways

Knowledge Unlocked

Five things to remember about Date.setHours()

Your foundation for local hour writes in JavaScript.

5
Core concepts
🕐 02

0–23

24-hour.

Range
📈 03

4 args

h, m, s, ms.

Overload
04

+ hours

getHours + n.

Pattern
🔄 05

Mutates

In place.

Behavior

❓ Frequently Asked Questions

It sets the hour (0–23) of a Date object in local time. Optional arguments can also set minutes, seconds, and milliseconds. The Date mutates in place and returns updated epoch milliseconds.
getHours() reads the current local hour. setHours(h) writes a new hour. They are the read/write pair for 24-hour local time.
24-hour format. Midnight is 0, noon is 12, and 11 PM is 23. Convert for AM/PM displays yourself or use toLocaleTimeString().
JavaScript normalizes overflow into the next calendar day. setHours(25) on March 1 becomes 01:xx on March 2 (minutes/seconds unchanged unless you pass them).
Yes. setHours(hour, minute, second, millisecond) updates all four time-of-day fields at once—useful for snapping to exact times like 15:00:00.000.
setHours() changes the local clock hour. setUTCHours() changes the UTC clock hour. Use the one that matches your timezone rules.
Did you know?

setHours(15, 0, 0, 0) sets 3:00 PM exactly and clears seconds and milliseconds—hour 15 is 24-hour time, not 3 PM in a separate AM/PM field.

Continue to setMilliseconds()

Learn how to set the sub-second fraction (0–999) on a Date in local time.

setMilliseconds() 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