JavaScript setHours(): Set or Change Hours

Date.prototype.setHours() changes the hour of an existing Date in local time. It can also set minutes, seconds and milliseconds in the same call. The Date is mutated, out-of-range values are normalized, and the method returns the updated timestamp.

Syntax:

date.setHours(hours);
date.setHours(hours, minutes, seconds, milliseconds);

Hours are normally 0–23:

const date = new Date(2026, 8, 10, 10, 30);
date.setHours(14);
console.log(date.getHours()); // 14

Setting the Hour Top ↑

Calling setHours(2) sets the local hour to 02:xx while keeping the existing minute, second and millisecond values.

const date = new Date();
date.setHours(2);
console.log(date.toString());

Setting Hours, Minutes, Seconds and Milliseconds Together Top ↑

Passing all four components is useful when you want an exact local clock time on the current calendar date.

const date = new Date();
date.setHours(9, 30, 0, 0);
console.log(date.toTimeString());

Negative Hours and Values Above 23 Top ↑

JavaScript normalizes the value. The original Plus2net examples using -2 and 25 are valid demonstrations: negative hours move into the previous day, while 24 or more moves into the next day.

const first = new Date(2026, 8, 10, 12);
first.setHours(-2);

const second = new Date(2026, 8, 10, 12);
second.setHours(25);

console.log(first);
console.log(second);

Adding or Subtracting Hours Top ↑

To move relative to the existing time, combine getHours() and setHours().

const date = new Date();
date.setHours(date.getHours() + 2);
console.log(date);

Return Value and Mutation Top ↑

const date = new Date();
const result = date.setHours(18);
console.log(typeof result); // "number"
console.log(date.getHours()); // 18

Daylight-Saving Time and Exact Elapsed Hours Top ↑

Because setHours() works in local time, crossing a daylight-saving change can produce an elapsed timestamp difference that is not exactly the number of hours multiplied by 3,600,000. For an exact elapsed duration, add milliseconds to the timestamp.

const date = new Date();
const twoHoursLater = new Date(date.getTime() + 2 * 60 * 60 * 1000);

Using setUTCHours() Top ↑

Use setUTCHours() when the hour components should be interpreted in UTC rather than the local time zone.

const date = new Date();
date.setUTCHours(12, 0, 0, 0);
console.log(date.toISOString());

Common setHours() Mistakes Top ↑

  • Using setHours(2) when you mean “add two hours”; use the current hour plus 2 for relative movement.
  • Assuming local-hour arithmetic always equals exact elapsed-hour arithmetic across DST.
  • Forgetting that omitted minutes/seconds remain unchanged.
  • Expecting the method to return a Date object.

Next: setMinutes() Date Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer