February 29 in a non-leap year in JavaScript
Beginner · 3 min read
February is the only month whose length changes: 28 days in most years and 29 in a leap year. In this tutorial you will write februaryDays(year), which answers 28 or 29. That part is short.
The more useful lesson is underneath it. What happens if you ask JavaScript for 29 February in a year that doesn't have one? You might expect an error. You get a different day, with no warning at all. Once you have seen it, you will know why a date form needs more checking than "did it build a date". You need to know how to make a date with new Date(year, month, day) and that months count from 0, so February is month 1.
Dates roll over
new Date never refuses a day that is too big. It counts on into the next month instead, like a car's odometer rolling over:
console.log(new Date(2026, 0, 32).toDateString()); // 'Sun Feb 01 2026'
32 January doesn't exist, so it becomes 1 February. The same thing works backwards: day 0 is the day before the 1st, which is the last day of the previous month.
Using day 0 to measure a month
That backwards roll gives a neat way to find a month's length. Day 0 of the next month is the last day of this month, and getDate() on the last day is the number of days:
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
console.log(daysInMonth(2026, 0)); // 31
Here is the exercise. Try it first if you like, then read on and we will build it together.
10 The length of February, and what 29 February does in a common year
easyReturn February's length for a given year. Then show what new Date(2023, 1, 29) actually produces.
Stuck? Three hints, smallest first
- 1. February is month 1, so its length is
daysInMonth(year, 1). - 2. The constructor never refuses an impossible day: it rolls forward.
- 3. So a 29 February that does not exist becomes 1 March, quietly.
Step 1: February's length
February is month 1, so its length is daysInMonth(year, 1):
const februaryDays = (year) => daysInMonth(year, 1);
console.log(februaryDays(2023), februaryDays(2024)); // 28 29
console.log(februaryDays(1900), februaryDays(2000)); // 28 29
1900 and 2000 are the tricky leap-year cases. Century years are only leap years when they divide by 400. The Date constructor knows this, so you get both right without writing the rule.
Step 2: ask for a day that doesn't exist
2023 is not a leap year. Build 29 February 2023 anyway:
const rolled = new Date(2023, 1, 29);
console.log(rolled.toDateString()); // 'Wed Mar 01 2023'
console.log(rolled.getMonth(), rolled.getDate()); // 2 1
No error, no Invalid Date, no warning. February 2023 has 28 days, so the 29th counts on one day into March. getMonth() is 2, which is March, and the day is 1.
Step 3: compare it with a real leap day
const real = new Date(2024, 1, 29);
console.log(real.toDateString()); // 'Thu Feb 29 2024'
console.log(real.getMonth(), real.getDate()); // 1 29
In 2024 the day exists, so it stays in February on the 29th.
Step 4: why this matters
Picture a sign-up form with three boxes: day, month and year. A user types 29, 2 and 2023. Your code builds new Date(2023, 1, 29), and it works, so you save it. The user's record now says 1 March. Nobody sees an error, and the mistake is only found much later, if at all.
So you can't check a date by asking "did the constructor succeed?". It always succeeds. Instead, ask "did I get back the day I asked for?":
const sameDay = (date, day) => date.getDate() === day;
console.log(sameDay(rolled, 29)); // false
console.log(sameDay(real, 29)); // true
A full check compares the year and month too. That is a later exercise, but the idea is this one line.
The full solution
The panel has daysInMonth, februaryDays and the two 29 February dates, printed side by side. Press Run and look at the line that says March.
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
const februaryDays = (year) => daysInMonth(year, 1);
// 29 February 2023 does not exist. The constructor does not complain.
const rolled = new Date(2023, 1, 29);
const real = new Date(2024, 1, 29);
console.log('February 2023:', februaryDays(2023), '| February 2024:', februaryDays(2024));
console.log('new Date(2023, 1, 29) ->', rolled.toDateString(), '<- March!');
console.log('new Date(2024, 1, 29) ->', real.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('February 2024 has 29 days', februaryDays(2024), 29);
test('February 2023 has 28', februaryDays(2023), 28);
test('February 1900 had 28', februaryDays(1900), 28);
test('February 2000 had 29', februaryDays(2000), 29);
test('29 February 2023 silently becomes 1 March', rolled.getDate(), 1);
test('in month 2, which is March', rolled.getMonth(), 2);
test('while in 2024 it stays in February', real.getMonth(), 1);
test('on the 29th', real.getDate(), 29);
test('so the day you asked for is not always the day you get', rolled.getDate() === 29, false);
February's length is easy. The lesson is underneath it: new Date(2023, 1, 29) does not throw, does not return Invalid Date, and does not warn. It returns 1 March. A form that accepts a day and a month and builds a date this way will happily accept 29 February 2023 and store the wrong day.
Validation therefore cannot be "did the constructor succeed" — it always succeeds. It has to be "did I get back the day I asked for", which is exercise 56.
Common mistakes
- Using month 2 for February. Month 2 is March, because months count from 0.
- Expecting an error for 29 February 2023. The constructor never complains; it rolls into 1 March.
- Treating "a date was built" as "the date is valid". Read the parts back and compare them with what you asked for.
Try it yourself
- What does
new Date(2023, 1, 30)give? Predict it first, then check. - Write
isRealDate(year, month, day)that builds the date and checks the year, month and day all match what you passed in. - Use
februaryDaysto list the years from 1896 to 1912 that have a 29 February. Which year is missing, and why?