Validate a year, month and day in JavaScript
Advanced · 4 min read
A sign-up form with three boxes for day, month and year. A spreadsheet import with dates in separate columns. A booking API that receives { year: 2026, month: 2, day: 30 }. Before you trust numbers like these, you need to know they make a real date. 30 February doesn't exist, and neither does 31 April.
In this tutorial you will write isRealDate(year, month, day), which answers true only for dates that exist. The month is 1 to 12 here, the way a form sends it. You need to know how to make a date from parts and that new Date counts months from 0.
new Date never says no
You might hope new Date would refuse an impossible date. It doesn't. It normalizes it, rolling the extra days into the next month:
console.log(new Date(2026, 3, 31).toDateString()); // 'Fri May 01 2026'
Month 3 is April, which has 30 days. So "31 April" quietly becomes 1 May. That is handy when you want to add days, but it means new Date alone can't tell you the input was wrong.
The round trip
Here is the trick. Build the date, then read the year, month and day back. If the date was real, nothing changed. If it rolled over, at least one part is different. That before-and-after check is called a round trip.
Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.
56 Is this year, month and day a real date?
hardValidate a year, a 1-to-12 month and a day before trusting them. new Date will not tell you: it rolls 31 April into 1 May.
Stuck? Three hints, smallest first
- 1. Reject non-integers and out-of-range months and days first, cheaply.
- 2. Then build the date and check all three fields come back unchanged.
- 3. The round trip is the only reliable test, because the constructor never refuses anything.
Step 1: whole numbers only
Form fields often send text or decimals. First make sure all three values are whole numbers:
console.log([2026, 1, 15].every(Number.isInteger)); // true
console.log([2026, 1, 1.5].every(Number.isInteger)); // false
console.log(['2026', 1, 15].every(Number.isInteger)); // false
every checks that a test passes for every item in the array. Number.isInteger is true only for whole numbers. The text '2026' fails too, which is what we want: convert form text with Number(...) before calling the function.
Step 2: quick range checks
Next, refuse values that can never be right:
const inRange = (month, day) => month >= 1 && month <= 12 && day >= 1 && day <= 31;
console.log(inRange(13, 1)); // false
console.log(inRange(1, 0)); // false
console.log(inRange(2, 30)); // true
Month 13 and day 0 are refused at once. But 30 February passes, because 30 is a valid day number in general. This check is cheap and makes the function easy to read, but it can't know month lengths. That is the round trip's job.
Step 3: the wrong turn, building and trusting
const built = new Date(2026, 2 - 1, 30);
console.log(built.getMonth(), built.getDate()); // 2 2
We asked for February (month 2, so 2 - 1 for the constructor) day 30. We got month 2, which is March, day 2. If the function just built the date and returned true, a booking for 30 February would be saved as 2 March.
Step 4: the round trip
function isRealDate(year, month, day) {
if (![year, month, day].every(Number.isInteger)) return false;
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
const date = new Date(year, month - 1, day);
return date.getFullYear() === year
&& date.getMonth() === month - 1
&& date.getDate() === day;
}
console.log(isRealDate(2026, 1, 15)); // true
console.log(isRealDate(2026, 2, 30)); // false
console.log(isRealDate(2026, 4, 31)); // false
The last three lines compare each part with what went in. 30 February rolled to 2 March, so the month and day don't match, and the answer is false.
Step 5: leap years come for free
console.log(isRealDate(2024, 2, 29)); // true
console.log(isRealDate(2026, 2, 29)); // false
console.log(isRealDate(2026, 12, 31)); // true
You didn't write a leap-year rule. new Date already knows that February 2024 has 29 days and February 2026 has 28, so the round trip handles it.
Step 6: the two-digit year
console.log(new Date(26, 0, 1).getFullYear()); // 1926
console.log(isRealDate(26, 1, 1)); // false
new Date treats years 0 to 99 as 1900 to 1999. So year 26 is built as 1926, the round trip sees a different year, and the answer is false. For a form that is the right result: someone who typed 26 almost certainly meant 2026, and should be asked to type all four digits.
The full solution
The panel has isRealDate. It prints 30 February, 29 February 2024 and 31 April, along with what new Date builds for the impossible ones. The checks cover month 13, day 0, a decimal day, 31 December and the two-digit year. Press Run to see them.
// month is 1-12 here, the way a form sends it.
function isRealDate(year, month, day) {
if (![year, month, day].every(Number.isInteger)) return false;
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
const date = new Date(year, month - 1, day);
return date.getFullYear() === year
&& date.getMonth() === month - 1
&& date.getDate() === day; // unchanged, so nothing was rolled
}
console.log('30/02/2026:', isRealDate(2026, 2, 30), '| new Date says', new Date(2026, 1, 30).toDateString());
console.log('29/02/2024:', isRealDate(2024, 2, 29));
console.log('31/04/2026:', isRealDate(2026, 4, 31), '| new Date says', new Date(2026, 3, 31).toDateString());
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('a real date passes', isRealDate(2026, 1, 15), true);
test('30 February does not', isRealDate(2026, 2, 30), false);
test('even though the constructor happily builds it', new Date(2026, 1, 30).getMonth(), 2);
test('29 February is real in a leap year', isRealDate(2024, 2, 29), true);
test('and not in a common one', isRealDate(2026, 2, 29), false);
test('31 April is not a date', isRealDate(2026, 4, 31), false);
test('month 13, day 0 and a fractional day are all refused', [isRealDate(2026, 13, 1), isRealDate(2026, 1, 0), isRealDate(2026, 1, 1.5)], [false, false, false]);
test('31 December is fine', isRealDate(2026, 12, 31), true);
test('a two-digit year is refused, because the constructor would read it as 19xx', isRealDate(26, 1, 1), false);
The round trip is the whole technique, and it works for the same reason exercise 19 was dangerous: the constructor normalises silently, so the only evidence that the input was impossible is that what came back is not what went in.
The cheap range checks in front are not redundant. Without the day < 1 guard, isRealDate(2026, 1, 0) would build 31 December 2025 and be caught by the round trip anyway — but relying on that makes the function harder to read and one refactor away from wrong.
The last assertion is a real limitation stated plainly rather than hidden: a two-digit year is rejected because new Date(26, 0, 1) is 1926, so the round trip fails. That is the right answer for form input, where 26 is a mistake. If you genuinely need years under 100, build with setFullYear as exercise 14 does.
Common mistakes
- Trusting
new Dateto refuse bad dates. It rolls them over instead. Always read the parts back. - Mixing up month numbering. Forms use 1 to 12,
new Dateuses 0 to 11. Subtract 1 when building and when comparing. - Writing your own month-length table. It is easy to get February wrong. Let the round trip use the calendar JavaScript already has.
Try it yourself
- Write
realDateOrNull(year, month, day)that returns the date when it is real andnullwhen it isn't. - Change the function to accept text from form fields, using
Number(...)first. - Count how many days of 2024 and 2026 pass
isRealDateby looping over every month and day from 1 to 31.