How to add days, months or hours to a date
Short answer
setDate() with the current day plus N moves the date forward and rolls over to the next month itself:
const date = new Date(2025, 0, 29);
date.setDate(date.getDate() + 5);
console.log(date.toDateString());Output:
Mon Feb 03 2025Without changing the original
The setters change the date object itself. Make a copy first:
const addDays = (date, days) => {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
const today = new Date(2025, 5, 10);
const nextWeek = addDays(today, 7);
console.log(today.getDate(), nextWeek.getDate());Output:
10 17Months and years
setMonth() and setFullYear() work the same way:
const date = new Date(2025, 10, 15);
date.setMonth(date.getMonth() + 3);
console.log(date.toDateString());Output:
Sun Feb 15 2026Hours and minutes
For hours and minutes, use setHours() and setMinutes():
const date = new Date(2025, 0, 1, 22, 30);
date.setHours(date.getHours() + 3);
console.log(date.toDateString(), date.getHours() + ':' + date.getMinutes());Output:
Thu Jan 02 2025 1:30Common mistake
Adding a month to January 31 gives March, not the end of February, because February 31 rolls over:
const date = new Date(2025, 0, 31);
date.setMonth(date.getMonth() + 1);
console.log(date.toDateString());Output:
Mon Mar 03 2025Functions used in this recipe
- Date.prototype.setDate — Sets the day of the month — handy for adding and subtracting days.
- Date.prototype.getDate — Returns the day of the month.