Mon Oct 25 2021·2 min read
Photo by Adib Harith Fadzilah
setHours() method to add hours to a date object. You can call the
getHours() method to get the hour for the specified date and use the addition
(+) operator to add as many hours as necessary to the date object.
// 👇️ current date const now = new Date(); now.setHours(now.getHours() + 1); // 👉️ add 1 Hour console.log(now); // 👇️ another date const date = new Date('September 24, 2025 07:24:00'); date.setHours(date.getHours() + 4); // 👉️ add 4 Hours console.log(date); // 👉️️ Wed Sep 24 2025 11:24:00
We used the Date.setHours method to set the hours for the date. The only parameter we passed to the method is a value for the hours.
Because we want to add hours to the date object, we used the Date.getHours to get the hour for the date and increased it.
Note that the date could roll over to the next day, e.g. if you add 2 hours and it's already 11 PM.
const date = new Date('September 24, 2025 23:24:00'); date.setHours(date.getHours() + 2); // 👉️ add 2 Hours console.log(date); // 👉️️ Wed Sep 25 2025 01:24:00
In the example, we added 2 hours to September 24th, 23:24 O'clock and the date rolled over to September 25th 01:24 O'clock.
setHoursmethod mutates the original date object, if you need to avoid this, you can create a copy of the date object and call the method on the copy.
const date = new Date('September 24, 2025 23:24:00'); const copy = new Date(date); copy.setHours(copy.getHours() + 2); // 👉️ add 2 Hours console.log(copy); // 👉️️ Wed Sep 25 2025 01:24:00 console.log(date); // 👉️ Wed Sep 24 2025 23:24:00
We created a copy of the date object, so we don't change it contents using the
I'll send you 1 email a week with links to all of the articles I've written that week