Date overflow: month 12 and day 32 in JavaScript
Intermediate · 4 min read
What day is the 32nd of January? In real life, there isn't one. In JavaScript, new Date(2026, 0, 32) happily gives you a date, and it is not an error. In this tutorial you will try four "impossible" dates (month 12, month -1, day 32 and day 0) and see exactly what each one turns into.
This behavior is called overflow, and it cuts both ways. It is a useful tool for date math, and a hidden trap for anything a user types in. By the end you will know when to use it and when to guard against it. You need to know how to make a date with new Date(year, month, day) and that months count from 0, so January is 0 and December is 11.
Dates carry like an odometer
A car's odometer shows 0999, then 1000: the extra carries into the next digit. new Date does the same with dates. If a part is too big, the extra carries into the next unit up. If it is too small, it borrows from the unit above.
- Too many days carry into the next month.
- Too many months carry into the next year.
- Too many hours carry into the next day.
Nothing is refused, and nothing warns you.
A helper to see the result
To compare results at a glance, print each date as YYYY-MM-DD using 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, 15))); // '2026-01-15'
The + 1 turns the zero-based month into the one people write, and padStart(2, '0') adds a leading zero when needed.
Here is the exercise. Try it yourself first, or read on and we will build it together.
19 Overflow: month 12 and day 32
fairFind out what new Date(2026, 12, 1), new Date(2026, -1, 1), new Date(2026, 0, 32) and new Date(2026, 0, 0) give you. None of them is an error.
Stuck? Three hints, smallest first
- 1. Out-of-range parts are not rejected, they are carried into the next or previous unit.
- 2. Month 12 is the thirteenth month, so it is January of the following year.
- 3. Day 0 is the day before the first, so it is the last day of the previous month.
Step 1: month 12
December is month 11, so month 12 is a thirteenth month. It carries into January of the next year:
console.log(iso(new Date(2026, 12, 1))); // '2027-01-01'
console.log(new Date(2026, 24, 1).getFullYear()); // 2028
Month 24 is 24 months after January 2026, which is exactly two years on.
Step 2: month -1
A negative month borrows from the year before. Month -1 is the month before January:
console.log(iso(new Date(2026, -1, 1))); // '2025-12-01'
Step 3: day 32
January has 31 days, so the 32nd carries into February:
console.log(iso(new Date(2026, 0, 32))); // '2026-02-01'
Step 4: day 0
Days start at 1, so day 0 is one day before the first. That is the last day of the previous month:
console.log(iso(new Date(2026, 0, 0))); // '2025-12-31'
console.log(new Date(2026, 2, 0).getDate()); // 28
The second line uses day 0 of March to land on the last day of February. Its getDate() is February's length. This is the standard trick for finding how many days a month has.
Step 5: hours carry too
It isn't just days and months. Hour 25 is one hour into the next day:
const late = new Date(2026, 0, 15, 25);
console.log(iso(late), late.getHours()); // 2026-01-16 1
Step 6: none of these are invalid
Every result above is a real, usable date. An invalid date has NaN as its timestamp, and none of these do:
const all = [new Date(2026, 12, 1), new Date(2026, -1, 1), new Date(2026, 0, 32), new Date(2026, 0, 0)];
console.log(all.every((d) => !Number.isNaN(d.getTime()))); // true
The useful side: date math
Overflow means you can add months without writing any "if December, go to next year" code:
console.log(iso(new Date(2026, 10 + 3, 1))); // '2027-02-01'
November (month 10) plus three months is February of the next year, and the constructor works that out.
The dangerous side: user input
Now imagine a form with day, month and year boxes. Someone types 32 by mistake. new Date(2026, 0, 32) gives 1 February, and your app saves it without a word. So when the parts come from a person, build the date, then check that getDate(), getMonth() and getFullYear() match what was typed. If they don't, the input was out of range.
The full solution
The panel builds all four dates, prints them with iso, and checks each one, plus hour 25 and the "month + 3" trick. Press Run to see them.
const iso = (d) => d.getFullYear() + '-' +
String(d.getMonth() + 1).padStart(2, '0') + '-' +
String(d.getDate()).padStart(2, '0');
const monthTwelve = new Date(2026, 12, 1); // the 13th month of 2026
const monthMinusOne = new Date(2026, -1, 1); // the month before January
const dayThirtyTwo = new Date(2026, 0, 32); // 32 January
const dayZero = new Date(2026, 0, 0); // the day before 1 January
console.log('new Date(2026, 12, 1) ->', iso(monthTwelve));
console.log('new Date(2026, -1, 1) ->', iso(monthMinusOne));
console.log('new Date(2026, 0, 32) ->', iso(dayThirtyTwo));
console.log('new Date(2026, 0, 0) ->', iso(dayZero));
console.log('new Date(2026, 0, 15, 25) ->', iso(new Date(2026, 0, 15, 25)), 'at hour', new Date(2026, 0, 15, 25).getHours());
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('month 12 rolls into the next year', iso(monthTwelve), '2027-01-01');
test('month -1 rolls back into the previous one', iso(monthMinusOne), '2025-12-01');
test('32 January is 1 February', iso(dayThirtyTwo), '2026-02-01');
test('day 0 is the last day of December', iso(dayZero), '2025-12-31');
test('and 24 months on is exactly two years on', new Date(2026, 24, 1).getFullYear(), 2028);
test('hour 25 spills into the next day', new Date(2026, 0, 15, 25).getDate(), 16);
test('the useful side: month + 3 needs no wrapping logic', iso(new Date(2026, 10 + 3, 1)), '2027-02-01');
test('nothing here is an Invalid Date', [monthTwelve, monthMinusOne, dayThirtyTwo, dayZero].every((d) => !Number.isNaN(d.getTime())), true);
Overflow is deliberate and it is documented: the constructor and every setter normalise their arguments rather than rejecting them. That cuts both ways. It is why new Date(y, m + 3, 1) can add a quarter without any December-to-January special case, and it is why a form that accepts 32 in the day field stores 1 February without a word of complaint.
So overflow is a tool for arithmetic and a hazard for input. Use it for the first and validate against it for the second.
Common mistakes
- Expecting an error for impossible parts.
new Datenever refuses them. It carries them into the next unit. - Thinking month 12 is December. December is 11. Month 12 is January of the next year.
- Trusting user input because a date was built. Read the parts back and compare them with what was typed.
Try it yourself
- Predict
new Date(2026, 0, -1), then check it. Which day is it? - Use overflow to write
addMonths(date, n)that returns a new datenmonths later. - Write
isRealDate(year, month, day)that builds the date and returnsfalseif any part changed.