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
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());
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());
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);
To move relative to the existing time, combine getHours() and setHours().
const date = new Date();
date.setHours(date.getHours() + 2);
console.log(date);
const date = new Date();
const result = date.setHours(18);
console.log(typeof result); // "number"
console.log(date.getHours()); // 18
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);
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());
setHours(2) when you mean “add two hours”; use the current hour plus 2 for relative movement.Next: setMinutes() Date Reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.