Add months to a date in JavaScript
Intermediate · 4 min read
"Your subscription renews in one month." "The loan is due in three months." Adding months to a date sounds simple, and most of the time it is. The trouble starts at the end of the month: what is one month after 31 January? February has no 31st.
In this tutorial you will write addMonths(date, n), which adds (or subtracts) months and lands on the last day of the month when the day doesn't fit. You'll see what the built-in setMonth does on its own, and why it overshoots. You need to know how to create a date with new Date(year, month, day). Remember that months count from 0: January is 0, February is 1.
How Date handles numbers that are too big
new Date never complains about a month or day out of range. It rolls the extra forward (or backward) instead.
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
console.log(iso(new Date(2026, 12, 15))); // 2027-01-15: month 12 is next January
console.log(iso(new Date(2026, -1, 15))); // 2025-12-15: month -1 is last December
console.log(iso(new Date(2026, 1, 31))); // 2026-03-03: day 31 of February rolls on
The iso helper just prints a date as yyyy-mm-dd so it's easy to read. We'll use it throughout.
The first two lines are good news: adding months never needs year arithmetic. The third line is the problem we have to solve.
The length of a month
To keep a day inside a month, we need to know how long that month is. Day 0 of a month means "the day before the 1st", which is the last day of the month before. So day 0 of the next month gives this month's length:
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
console.log(daysInMonth(2026, 1)); // 28
console.log(daysInMonth(2028, 1)); // 29
Here is the exercise. Try it yourself first if you like, or read on and we will build it together.
127 Add a month and stay in the right month
fairAdd a number of months to a date. Adding one month to 31 January should give the last day of February, not the 3rd of March. Show what setMonth does on its own, and make the version that crosses a year boundary work too.
Stuck? Three hints, smallest first
- 1. The month argument may be out of 0 to 11: month 12 is January of the next year, month -1 is December of the last.
- 2.
setMonthkeeps the day of the month, which is exactly the problem. - 3. Find the length of the target month first, then clamp the day into it.
Step 1: what setMonth does
The obvious approach is to take the date and move its month forward by one:
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
const naive = new Date(2026, 0, 31);
naive.setMonth(naive.getMonth() + 1);
console.log(iso(naive)); // 2026-03-03
setMonth keeps the day, 31, and asks for 31 February. That rolls three days into March. A customer who signed up on 31 January would be billed on 3 March, skipping February entirely.
Notice also that setMonth changes the date object itself. That is called mutating it.
Step 2: build a new date with the month added
Instead of changing the original, read its parts and build a fresh date:
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
function addMonths(date, n) {
const year = date.getFullYear();
const month = date.getMonth() + n;
return new Date(year, month, date.getDate());
}
console.log(iso(addMonths(new Date(2026, 0, 15), 1))); // 2026-02-15
console.log(iso(addMonths(new Date(2026, 11, 15), 1))); // 2027-01-15
console.log(iso(addMonths(new Date(2026, 0, 31), 1))); // 2026-03-03, still wrong
The year rolls on its own, as promised: month 12 of 2026 is January 2027. But 31 January still overshoots, because we haven't dealt with short months yet.
Step 3: clamp the day
Work out how long the target month is, and use Math.min to keep the day from going past its end:
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
function addMonths(date, n) {
const year = date.getFullYear();
const month = date.getMonth() + n;
const last = daysInMonth(year, month);
return new Date(year, month, Math.min(date.getDate(), last));
}
console.log(iso(addMonths(new Date(2026, 0, 31), 1))); // 2026-02-28
console.log(iso(addMonths(new Date(2028, 0, 31), 1))); // 2028-02-29
console.log(iso(addMonths(new Date(2026, 2, 31), -1))); // 2026-02-28
console.log(iso(addMonths(new Date(2026, 1, 28), 12))); // 2027-02-28
daysInMonth(year, month) works even when month is 13 or -1, because Date rolls those too. In a leap year the clamp lands on the 29th, and going backwards from 31 March clamps in the same way.
Clamping means forcing a value into a range. Here the range is 1 to the last day of the month.
Step 4: twice one month is not two months
Here is a surprise worth seeing:
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
function addMonths(date, n) {
const year = date.getFullYear();
const month = date.getMonth() + n;
return new Date(year, month, Math.min(date.getDate(), daysInMonth(year, month)));
}
const endOfJanuary = new Date(2026, 0, 31);
console.log(iso(addMonths(addMonths(endOfJanuary, 1), 1))); // 2026-03-28
console.log(iso(addMonths(endOfJanuary, 2))); // 2026-03-31
The first line adds one month, gets 28 February, then adds one month to that, and the 31 is gone for good. The clamp throws information away.
That isn't a bug in addMonths. It's how months work. So for a repeating date, like a monthly bill, store the original day (31) and always add months to the first date: addMonths(start, 1), addMonths(start, 2), and so on. Don't keep adding to the last result.
The full solution
The answer panel has daysInMonth, iso and the clamped addMonths, and prints the clamped result next to what setMonth gives. The checks cover leap years, a day that fits, crossing the year in both directions, twelve months, and the "twice is not two" case. Press Run to see the output.
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
const iso = (d) =>
d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0');
// A month index outside 0..11 rolls the year for free, so no year arithmetic is
// needed. The only work is clamping the day into the month it is landing in.
function addMonths(date, n) {
const year = date.getFullYear();
const month = date.getMonth() + n;
const last = daysInMonth(year, month);
return new Date(year, month, Math.min(date.getDate(), last));
}
// What setMonth does by itself, which is where the bug comes from.
const naive = new Date(2026, 0, 31);
naive.setMonth(naive.getMonth() + 1);
const endOfJanuary = new Date(2026, 0, 31);
console.log('31 Jan 2026 plus a month, clamped:', iso(addMonths(endOfJanuary, 1)));
console.log('the same thing with setMonth: ', iso(naive));
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('31 January plus one month is the end of February', iso(addMonths(endOfJanuary, 1)), '2026-02-28');
test('setMonth on its own overshoots into March', iso(naive), '2026-03-03');
test('in a leap year the clamp lands on the 29th', iso(addMonths(new Date(2028, 0, 31), 1)), '2028-02-29');
test('a month with room for the day keeps it', iso(addMonths(new Date(2026, 0, 15), 1)), '2026-02-15');
test('past December the year rolls on its own', iso(addMonths(new Date(2026, 11, 15), 1)), '2027-01-15');
test('and a negative count rolls back the same way', iso(addMonths(new Date(2026, 0, 15), -1)), '2025-12-15');
test('going back from 31 March clamps too', iso(addMonths(new Date(2026, 2, 31), -1)), '2026-02-28');
test('twelve months is the same day next year', iso(addMonths(new Date(2026, 1, 28), 12)), '2027-02-28');
test('and adding one month twice is not the same as adding two', [iso(addMonths(addMonths(endOfJanuary, 1), 1)), iso(addMonths(endOfJanuary, 2))], ['2026-03-28', '2026-03-31']);
Two facts do the work. A month index outside 0 to 11 is legal — month 12 means January of the next year, month -1 means December of the last — so adding months needs no year arithmetic at all. And the day must be clamped before the date is built, because Date will happily accept a 31st in a 28-day month and walk forward into March.
The last assertion is the one to sit with: adding a month twice gives 28 March, adding two months at once gives 31 March. Month arithmetic is not associative once clamping is involved, because the clamp throws information away. That is not a bug in this function, it is the shape of the problem — which is why a billing system stores the original anniversary day (31) and clamps at render time, instead of clamping once and then adding to the clamped result month after month, sliding every subsequent date to the 28th.
Common mistakes
- Using
setMonthalone. It keeps the day, so the 29th, 30th or 31st can roll into the following month. - Doing your own year arithmetic. Month 12 and month -1 already roll the year. Extra code here only adds bugs.
- Adding to the last result for repeating dates. After one short month every later date slides to the 28th.
Try it yourself
- Write
addYears(date, n)usingaddMonths. What does 29 February 2028 plus one year give? - List the next six billing dates for a customer who started on 31 January 2026, always adding to the start date.
- Write
monthsBetween(a, b)that counts whole months between two dates.