Add one month to January 31 in JavaScript
Intermediate · 4 min read
What is one month after 31 January? Most people say 28 February, or the 29th in a leap year. JavaScript's obvious one-liner says 3 March. Monthly bills, subscription renewals and "remind me in a month" features all hit this case, so it is worth getting right.
In this tutorial you will write the obvious version, see exactly why it skips February, and then fix it with addMonths(date, months). The fix clamps the day: if the target month is too short, it stops on that month's last day. 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.
setMonth keeps the day
setMonth(month) changes the month and leaves the day of the month as it was. That is fine for the 15th, which every month has. It is a problem for the 29th, 30th and 31st.
When a date ends up on a day that doesn't exist, JavaScript doesn't complain. It rolls the extra days into the next month. "31 February" is three days past 28 February in 2026, so it becomes 3 March.
console.log(new Date(2026, 1, 31).getMonth()); // 2: March, not February
How long is a month?
The fix needs to know how many days a month has. There is a neat trick: day 0 of a month means "the day before the 1st", which is the last day of the month before. So day 0 of next month is the last day of this month:
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
console.log(daysInMonth(2026, 1)); // 28
console.log(daysInMonth(2024, 1)); // 29
Month 1 is February. 2024 is a leap year, so it gets 29.
To print results, this tutorial uses iso, a small helper that turns a date into YYYY-MM-DD text:
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
Here is the exercise. If you'd like to try it first, go ahead. Otherwise, read on and we will build it together.
24 Add months, and clamp the overflow
fairAdd one month to 31 January 2026. The obvious code gives you 3 March. Fix it so it gives 28 February, and show both.
Stuck? Three hints, smallest first
- 1.
setMonthkeeps the day of the month, so 31 February normalises into March. - 2. Park the copy on day 1 before changing the month, so nothing can overflow while you do it.
- 3. Then put the day back, clamped to the length of the month you have landed in.
Step 1: the obvious version
Copy the date and add to its month:
function naiveAddMonths(date, months) {
const copy = new Date(date);
copy.setMonth(copy.getMonth() + months);
return copy;
}
const endOfJanuary = new Date(2026, 0, 31);
console.log(iso(naiveAddMonths(endOfJanuary, 1))); // '2026-03-03'
The copy moves to February and keeps day 31. February 2026 has 28 days, so the date rolls three days into March. There is no error, just a date in the wrong month.
Step 2: park on day 1 first
The overflow happens at the moment the month changes. So before changing the month, move the copy to day 1, which every month has. Remember the real day first, so you can put it back:
function addMonths(date, months) {
const day = date.getDate();
const copy = new Date(date);
copy.setDate(1); // a day every month has
copy.setMonth(copy.getMonth() + months);
return copy;
}
console.log(iso(addMonths(endOfJanuary, 1))); // '2026-02-01'
Now the copy lands in February, as it should. It is on the wrong day for now, but it is in the right month.
Step 3: put the day back, clamped
The day you want is the original day, unless the new month is too short. Math.min picks the smaller of two numbers, so Math.min(day, length of month) gives exactly that:
function addMonths(date, months) {
const day = date.getDate();
const copy = new Date(date);
copy.setDate(1);
copy.setMonth(copy.getMonth() + months);
copy.setDate(Math.min(day, daysInMonth(copy.getFullYear(), copy.getMonth())));
return copy;
}
console.log(iso(addMonths(endOfJanuary, 1))); // '2026-02-28'
console.log(iso(addMonths(new Date(2024, 0, 31), 1))); // '2024-02-29'
console.log(iso(addMonths(new Date(2026, 0, 15), 1))); // '2026-02-15'
For 31 January, the smaller of 31 and 28 is 28. In 2024, February has 29 days, so you get the 29th. The 15th exists in every month, so it stays the 15th.
Step 4: more months, and going back
The same function handles long jumps and negative counts:
console.log(iso(addMonths(endOfJanuary, 13))); // '2027-02-28'
console.log(iso(addMonths(endOfJanuary, 12))); // '2027-01-31'
console.log(iso(addMonths(new Date(2026, 2, 31), -1))); // '2026-02-28'
console.log(iso(endOfJanuary)); // '2026-01-31'
Month numbers past 11 roll into the next year, so 13 months on is February 2027. One month back from 31 March clamps to 28 February. The input never changes, because only the copy is touched.
Step 5: clamping loses the day
Clamping is a choice, and it has a cost. Go forward a month and then back a month:
console.log(iso(addMonths(addMonths(endOfJanuary, 1), -1))); // '2026-01-28'
You started on the 31st and ended on the 28th. The trip through February forgot the original day. If a subscription must renew on the 31st whenever the month allows it, store the original day and work out each renewal from the start date, instead of stepping from the last result.
The full solution
The panel has both versions side by side. It shows the naive one landing in March, then checks the clamped one across short months, leap years, long jumps and going backwards. Press Run to see the output.
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
// The obvious version. 31 January + 1 month asks for 31 February.
function naiveAddMonths(date, months) {
const copy = new Date(date);
copy.setMonth(copy.getMonth() + months);
return copy;
}
function addMonths(date, months) {
const day = date.getDate();
const copy = new Date(date);
copy.setDate(1); // a day every month has
copy.setMonth(copy.getMonth() + months);
copy.setDate(Math.min(day, daysInMonth(copy.getFullYear(), copy.getMonth())));
return copy;
}
const endOfJanuary = new Date(2026, 0, 31);
console.log('naive 31/01/2026 + 1 month ->', iso(naiveAddMonths(endOfJanuary, 1)), '<- March');
console.log('clamped 31/01/2026 + 1 month ->', iso(addMonths(endOfJanuary, 1)));
console.log('clamped 31/01/2024 + 1 month ->', iso(addMonths(new Date(2024, 0, 31), 1)), '<- leap year');
Output
8 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 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('the naive version skips February entirely', iso(naiveAddMonths(endOfJanuary, 1)), '2026-03-03');
test('the clamped version stops at the end of February', iso(addMonths(endOfJanuary, 1)), '2026-02-28');
test('and gains a day in a leap year', iso(addMonths(new Date(2024, 0, 31), 1)), '2024-02-29');
test('a day that exists in both months is untouched', iso(addMonths(new Date(2026, 0, 15), 1)), '2026-02-15');
test('thirteen months on crosses the year', iso(addMonths(endOfJanuary, 13)), '2027-02-28');
test('going backwards clamps too', iso(addMonths(new Date(2026, 2, 31), -1)), '2026-02-28');
test('twelve months on is the same date a year later', iso(addMonths(endOfJanuary, 12)), '2027-01-31');
test('and the input is untouched', iso(endOfJanuary), '2026-01-31');
setMonth changes the month and leaves the day where it was, so any day past the 28th can overflow. 31 January plus one month asks for 31 February, which normalises to 3 March — a date in the wrong month, with no error.
Clamping is the usual fix and it is what almost every library does, but be clear that it is a choice: it makes the operation lossy, so addMonths(addMonths(jan31, 1), -1) is 28 January rather than 31 January. If a subscription has to bill on the 31st where the month allows it, keep the original day of the month somewhere and re-derive from that each cycle instead of stepping from the last result. Exercise 54 shows the difference.
Common mistakes
- Calling
setMonthwith the day still on 31. The date overflows before you get a chance to fix it. Move to day 1 first. - Clamping after the month has changed. Once the date has rolled into March,
getMonth()already says March, so the clamp uses the wrong month. - Stepping month by month from the last result. Each short month can shave the day down, and it never comes back.
Try it yourself
- Print
addMonths(new Date(2026, 0, 31), n)fornfrom 1 to 12. Which months land on the 31st? - Write
subtractMonths(date, n)in one line usingaddMonths. - Write
renewalDate(start, n)that always works from the start date, so the 31st comes back in months that have one.