Add days to a date in JavaScript
Intermediate · 4 min read
"Due in 14 days", "free trial ends in 7 days", "yesterday's orders": adding and subtracting days is one of the most common jobs you will do with dates. In this tutorial you will write addDays(date, n). It returns a new date n days later, or earlier when n is negative, and it never changes the date you pass in.
The good news is that you don't need to know how long any month is. JavaScript works out month ends, year ends and leap years for you. You need to know how to make a date with new Date(year, month, day), that months count from 0, and that a date should be copied before you change it.
Days roll over on their own
Every date has a setDate(day) method that sets the day of the month. If you give it a day that doesn't exist in that month, the extra days carry into the next month. Day 32 of January is 1 February. Day 0 of January is the last day of December.
const d = new Date(2026, 0, 1);
d.setDate(32);
console.log(d.getMonth(), d.getDate()); // 1 1
Month 1 is February, so d is now 1 February 2026. That rollover is what makes addDays short. To move on by n days, set the day to "today's day plus n" and let JavaScript sort out the rest.
A helper to print dates
To check results at a glance, this tutorial prints dates as YYYY-MM-DD, built from the local getters:
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
console.log(iso(new Date(2026, 0, 31))); // '2026-01-31'
getMonth() counts from 0, so it adds 1 back. padStart(2, '0') turns 1 into '01'.
Here is the exercise. Have a go on your own first if you like, then read on and we will build it step by step.
22 Add and subtract days
fairWrite addDays(date, n) that returns a new date n days later, leaving the input alone. Negative n goes backwards.
Stuck? Three hints, smallest first
- 1. Copy, then
setDate(getDate() + n)on the copy. - 2. You do not need any month-end logic: the setter normalises out-of-range days for you.
- 3. Do not add
n * 86400000milliseconds. That is a different operation, and exercise 53 shows where the two part company.
Step 1: copy the date
setDate changes the date it is called on. If addDays called it on the input, the caller's date would move too. So the first line makes a copy:
function addDays(date, days) {
const copy = new Date(date);
return copy;
}
new Date(date) builds a separate date for the same moment. Everything after this line changes copy, never date.
Step 2: move the copy on
Read the copy's day of the month, add days, and set it back:
function addDays(date, days) {
const copy = new Date(date);
copy.setDate(copy.getDate() + days); // out-of-range days roll into the next month
return copy;
}
const endOfJanuary = new Date(2026, 0, 31);
console.log(iso(addDays(endOfJanuary, 1))); // '2026-02-01'
console.log(iso(endOfJanuary)); // '2026-01-31'
31 plus 1 is 32, and day 32 of January rolls into 1 February. The input is still 31 January, because only the copy changed.
Step 3: cross a year, and go backwards
The same two lines handle the end of the year and negative counts:
console.log(iso(addDays(new Date(2026, 11, 31), 1))); // '2027-01-01'
console.log(iso(addDays(new Date(2026, 0, 1), -1))); // '2025-12-31'
console.log(iso(addDays(endOfJanuary, 0))); // '2026-01-31'
Day 32 of December carries into January of the next year. For -1, the code asks for day 0 of January, which is the day before the 1st: 31 December of the year before. Adding zero gives back the same date, as you would hope.
Step 4: leap years come for free
February has 28 days in most years and 29 in a leap year. addDays doesn't need to know which:
console.log(iso(addDays(new Date(2024, 1, 28), 1))); // '2024-02-29'
console.log(iso(addDays(new Date(2026, 1, 28), 1))); // '2026-03-01'
2024 is a leap year, so the day after 28 February is the 29th. 2026 isn't, so it is 1 March.
Step 5: a year is not always 365 days
It is tempting to write "a year later" as addDays(date, 365). That works until a 29 February falls inside the span:
console.log(iso(addDays(new Date(2026, 0, 15), 365))); // '2027-01-15'
console.log(iso(addDays(new Date(2024, 0, 15), 365))); // '2025-01-14'
console.log(iso(addDays(new Date(2024, 0, 15), 366))); // '2025-01-15'
From 15 January 2024, the next 365 days include 29 February, so you land a day short. If you mean "the same date next year", add years instead of days.
The wrong turn: adding milliseconds
Another idea is to add days * 86400000 to the timestamp, since a day has 86,400,000 milliseconds. That adds 24-hour blocks of time, which is a different operation. In places that move their clocks for daylight saving, one day a year is 23 hours long and another is 25, so adding 24 hours can land on the wrong day or at the wrong time. setDate moves the calendar date and keeps the clock time, which is what "add a day" usually means.
The full solution
The panel has the iso helper and the two-line addDays, then checks month ends, year ends, negative counts, leap years and the 365 versus 366 case. Press Run to see the output.
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
function addDays(date, days) {
const copy = new Date(date);
copy.setDate(copy.getDate() + days); // out-of-range days roll into the next month
return copy;
}
const endOfJanuary = new Date(2026, 0, 31);
console.log('31/01/2026 + 1 ->', iso(addDays(endOfJanuary, 1)));
console.log('31/12/2026 + 1 ->', iso(addDays(new Date(2026, 11, 31), 1)));
console.log('01/01/2026 - 1 ->', iso(addDays(new Date(2026, 0, 1), -1)));
console.log('the input is still', iso(endOfJanuary));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('one day past the end of January', iso(addDays(endOfJanuary, 1)), '2026-02-01');
test('one day past the end of the year', iso(addDays(new Date(2026, 11, 31), 1)), '2027-01-01');
test('a negative count goes backwards', iso(addDays(new Date(2026, 0, 1), -1)), '2025-12-31');
test('into 29 February in a leap year', iso(addDays(new Date(2024, 1, 28), 1)), '2024-02-29');
test('but straight to 1 March in a common one', iso(addDays(new Date(2026, 1, 28), 1)), '2026-03-01');
test('zero days is the same date', iso(addDays(endOfJanuary, 0)), '2026-01-31');
test('the input was not modified', iso(endOfJanuary), '2026-01-31');
test('365 days on from 15 January 2026', iso(addDays(new Date(2026, 0, 15), 365)), '2027-01-15');
test('and 366 days on from a date in a leap year', iso(addDays(new Date(2024, 0, 15), 366)), '2025-01-15');
Two lines, and all the month-end and year-end work is done for you by the same normalising behaviour that made exercise 19 dangerous. setDate(32) in January is 1 February, so addDays needs no idea how long any month is.
The last two assertions are a reminder that a year is not a fixed number of days: 365 days from a date in 2026 lands on the same date, but you have to use 366 when a 29 February falls inside the span. If you mean "a year later", say addYears — exercise 25 — rather than counting days.
Common mistakes
- Changing the input. Calling
date.setDate(...)without a copy moves the caller's date as well. - Writing
setDate(days). That sets the day of the month todays. To add, usecopy.getDate() + days. - Adding 365 days for "next year". It lands a day early when the span includes 29 February.
Try it yourself
- Use
addDaysto find the date 100 days after 1 January 2026. - Write
subtractDays(date, n)in one line that callsaddDays. - Write
tomorrow(date)andyesterday(date), and check them on 1 March 2024 and 1 March 2026.