JavaScript Date setHours() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Date

Definition

Date.prototype.setHours() changes the hour (and optionally minutes, seconds, milliseconds) of a date in local time. It mutates the Date in place and returns the new timestamp.

Remember
date.setHours(hours)
date.setHours(hours, minutes)
date.setHours(hours, minutes, seconds)
date.setHours(hours, minutes, seconds, ms)

hours: 0 … 23   (local)
→ returns new timestamp (ms)
→ mutates date

setHours(20)        // keep minutes/seconds
setHours(20, 21, 22) // set H:M:S together

Syntax

Call date.setHours. Start with this line.

JavaScript
date.setHours(h[, min[, sec[, ms]]])
CallWhat it does
date.setHours(h[, min[, sec[, ms]]])Primary signature.
date.setHours(...)Call with the arguments your case needs.

Built into JavaScript. Call setHours(...) on a Date instance — no import required.

Library declarations
JavaScript
date.setHours(h[, min[, sec[, ms]]])

Parameters

ParameterDescription
hoursValueRequired. Local hour (typically 0–23; overflow allowed)
minutesValueOptional. Local minutes (0–59); keep current if omitted
secondsValue / msValueOptional. Must provide earlier parts if you pass these

Return Value

Return ValueDescription
*The value returned by date.setHours().

JavaScript Examples

Example 1 — Set the Hour Only

Change the hour and keep the existing minutes and seconds.

JavaScript
const event = new Date(1975, 7, 19, 23, 15, 30);

event.setHours(20);

console.log(event.getHours());   // 20
console.log(event.getMinutes()); // 15
Try It Yourself

How It Works

  1. Start at 23:15:30. Month index 7 is August.
  2. Call setHours(20). Only the hour argument is provided.
  3. Minutes stay. Because minutes were omitted, they remain 15.
  4. Object mutated. The same event now reads hour 20.

Example 2 — Set Hours, Minutes, and Seconds Together

Pass multiple arguments to snap to an exact local time of day.

JavaScript
const event = new Date(1975, 7, 19, 23, 15, 30);

event.setHours(20, 21, 22);

console.log(event.getHours());   // 20
console.log(event.getMinutes()); // 21
console.log(event.getSeconds()); // 22
Try It Yourself

How It Works

  1. Start from the same date. Local time is 23:15:30.
  2. Pass three numbers. setHours(20, 21, 22) sets hour, minute, and second.
  3. All three update. Result is 20:21:22 local.
  4. Why it helps. One call snaps to a precise wall-clock time without separate setters.

Example 3 — Hour Overflow Rolls to the Next Day

Values outside 0–23 normalize into later calendar days.

JavaScript
const d = new Date(2026, 2, 15, 10, 0, 0); // Mar 15, 2026 10:00

d.setHours(25);

console.log(d.getDate());  // 16
console.log(d.getHours()); // 1
Try It Yourself

How It Works

  1. Start March 15 at 10:00. Local hour is 10.
  2. Ask for hour 25. That is past the end of the day.
  3. Engine normalizes. 25 hours from midnight = next day 01:00.
  4. Read back. Date becomes the 16th; hour becomes 1.

Browser & Runtime Support

Date.prototype.setHours() has been in JavaScript since ES1. It is Baseline and works in every modern browser and Node.js — no polyfill needed.

✓ Baseline · ES1

Date.prototype.setHours()

Supported everywhere Date exists: Chrome, Firefox, Safari, Edge, Internet Explorer, and all Node.js versions.

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 to use in any environment. Watch mutation and DST when adjusting local hours. See also the MDN setHours() reference.

Frequently Asked Questions

It changes the hour (0–23) of a Date in local time. Optional arguments can also set minutes, seconds, and milliseconds. The Date mutates in place and the method returns the new timestamp in milliseconds.
getHours() reads the local hour. setHours(h) writes a new local 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.
JavaScript normalizes overflow into the next day. setHours(25) becomes 01:xx on the following calendar day (other fields keep or update based on what you passed).
Yes. setHours(hour, minute, second, ms) updates all time-of-day fields at once—useful for snapping to an exact time like 15:00:00.000.
setHours() changes the local clock. setUTCHours() changes the UTC clock. Use the one that matches your timezone rules. For fixed elapsed-time math across DST, prefer setUTCHours() or setTime().

Next: setMilliseconds()

Learn how to set the milliseconds portion of 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.

11 people found this page helpful