Emmanuel Akinfulubi

JavaScriptDate

Add years to a date (leap day) in JavaScript

Intermediate · 4 min read

Someone born on 29 February 2024 has a problem in 2025: there is no 29 February that year. Anniversaries, yearly renewals and "expires in one year" dates all need an answer for this case. JavaScript's obvious one-liner picks 1 March without telling you.

In this tutorial you will write addYears(date, years). It adds whole years and, when the date would land on a day that doesn't exist, it stops on 28 February instead. You will also see why "four years later" isn't always safe either. You need to know how to make a date with new Date(year, month, day), that months count from 0 (so February is 1), and that a date should be copied before you change it.

Leap years in one paragraph

A leap year has 366 days, with the extra day on 29 February. The rule: a year that divides by 4 is a leap year, except century years (like 1900 or 2100), which are leap years only if they also divide by 400. So 2024 and 2028 are leap years, 2025 is not, 2000 was, and 2100 won't be.

setFullYear keeps the month and day

setFullYear(year) changes the year and leaves the month and day alone. If the result is a day that doesn't exist, JavaScript rolls it forward into the next month:

JavaScript

const d = new Date(2024, 1, 29);
d.setFullYear(2025);
console.log(d.getMonth(), d.getDate()); // 2 1

Month 2 is March, so this is 1 March 2025. No error, no warning.

You will also need two helpers. daysInMonth uses the fact that day 0 of next month is the last day of this month. iso prints a date as YYYY-MM-DD:

JavaScript

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');
console.log(daysInMonth(2025, 1)); // 28

Here is the exercise. Try it yourself first if you like, then read on and we will build it step by step.

25 Add years, and what happens to 29 February

fair

Add a year to 29 February 2024. Show what setFullYear does on its own, and clamp it to 28 February 2025 instead.

Stuck? Three hints, smallest first
  1. 1. setFullYear keeps the month and day, so 29 February in a common year overflows.
  2. 2. The same trick as adding months: park on day 1, change the year, then put the day back clamped.
  3. 3. Four years on it fits again, because leap years come in fours — except at a century.

Step 1: the obvious version

Copy the date, then add to the year:

Step 1

function naiveAddYears(date, years) {
  const copy = new Date(date);
  copy.setFullYear(copy.getFullYear() + years);
  return copy;
}
const leapDay = new Date(2024, 1, 29);   // 29 February 2024
console.log(iso(naiveAddYears(leapDay, 1))); // '2025-03-01'

This is the rollover from above: 29 February 2025 doesn't exist, so it becomes 1 March. For most dates the naive version is fine. It only goes wrong on a leap day.

Step 2: park on day 1, then change the year

The fix is the same one used for adding months. Remember the day, move the copy to day 1, and only then change the year. Day 1 exists in every month, so nothing can roll over:

Step 2

function addYears(date, years) {
  const day = date.getDate();
  const copy = new Date(date);
  copy.setDate(1);
  copy.setFullYear(date.getFullYear() + years);
  return copy;
}
console.log(iso(addYears(leapDay, 1))); // '2025-02-01'

The copy is in February 2025 now, which is the right month. Next, put the day back.

Step 3: put the day back, clamped

Math.min picks the smaller of two numbers. Use it to choose between the original day and the length of the month you landed in:

Step 3

function addYears(date, years) {
  const day = date.getDate();
  const copy = new Date(date);
  copy.setDate(1);
  copy.setFullYear(date.getFullYear() + years);
  copy.setDate(Math.min(day, daysInMonth(copy.getFullYear(), copy.getMonth())));
  return copy;
}
console.log(iso(addYears(leapDay, 1))); // '2025-02-28'
console.log(iso(leapDay));              // '2024-02-29'

February 2025 has 28 days, and the smaller of 29 and 28 is 28. The input is still 29 February 2024, because only the copy changed.

Step 4: when the leap day fits again

Four years on is 2028, a leap year, so the 29th exists and the clamp does nothing:

Step 4

console.log(iso(addYears(leapDay, 4)));  // '2028-02-29'
console.log(iso(addYears(leapDay, 76))); // '2100-02-28'

76 is a multiple of four, so you might expect 2100 to have a 29 February too. It doesn't. 2100 divides by 100 but not by 400, so it isn't a leap year. The clamp catches it and gives 28 February. This is why a hand-written rule like "add multiples of four and you are safe" eventually breaks.

Step 5: ordinary dates are unaffected

For any date that isn't 29 February, clamping never kicks in:

Step 5

console.log(iso(addYears(new Date(2026, 0, 15), 1)));  // '2027-01-15'
console.log(iso(addYears(new Date(2026, 0, 15), -1))); // '2025-01-15'

A negative count goes back in time, using the same steps.

28 February or 1 March?

There is no single right answer for a leap-day birthday in a common year. Clamping to 28 February keeps the anniversary in the same month, which is what this exercise asks for. Some systems use 1 March instead. Whichever you pick, pick it on purpose and write a test for it, as the full solution does.

The full solution

The panel puts the naive and clamped versions side by side, then checks one year on, four years on, the year 2100 and ordinary dates in both directions. Press Run to see the results.

The answer

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();

function naiveAddYears(date, years) {
  const copy = new Date(date);
  copy.setFullYear(copy.getFullYear() + years);
  return copy;
}

function addYears(date, years) {
  const day = date.getDate();
  const copy = new Date(date);
  copy.setDate(1);
  copy.setFullYear(date.getFullYear() + years);
  copy.setDate(Math.min(day, daysInMonth(copy.getFullYear(), copy.getMonth())));
  return copy;
}

const leapDay = new Date(2024, 1, 29);   // 29 February 2024

console.log('naive   29/02/2024 + 1 year ->', iso(naiveAddYears(leapDay, 1)), '<- March');
console.log('clamped 29/02/2024 + 1 year ->', iso(addYears(leapDay, 1)));
console.log('clamped 29/02/2024 + 4 years->', iso(addYears(leapDay, 4)), '<- fits again');

Output


      
7 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 7 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

7 checks

test('the naive version lands in March', iso(naiveAddYears(leapDay, 1)), '2025-03-01');
test('the clamped version stays in February', iso(addYears(leapDay, 1)), '2025-02-28');
test('four years on, 29 February exists again', iso(addYears(leapDay, 4)), '2028-02-29');
test('76 years on it does not, because 2100 is not a leap year', iso(addYears(leapDay, 76)), '2100-02-28');
test('an ordinary date is unaffected', iso(addYears(new Date(2026, 0, 15), 1)), '2027-01-15');
test('and going backwards works', iso(addYears(new Date(2026, 0, 15), -1)), '2025-01-15');
test('the input is untouched', iso(leapDay), '2024-02-29');

Same shape of problem as adding months, same fix. The case that makes it real is a birthday: someone born on 29 February 2024 has no 29 February in 2025, so your code has to decide between 28 February and 1 March. Clamping down to 28 February is the common choice and the one most legal definitions of "anniversary" use.

The 2100 assertion is there because 76 years is a multiple of four, so the naive expectation is that 29 February exists — and it does not, because 2100 is divisible by 100 and not by 400. The leap rule from exercise 9 is doing real work here.

Common mistakes

  • Calling setFullYear while the day is still 29. The date rolls into March before you can clamp it. Move to day 1 first.
  • Assuming every fourth year is a leap year. 2100 isn't. Let daysInMonth decide instead of a rule of thumb.
  • Adding 365 days instead of a year. That lands a day early whenever the span includes 29 February.

Try it yourself

  1. Print addYears(leapDay, n) for n from 1 to 8. Which years keep the 29th?
  2. Change addYears so a leap day moves to 1 March in common years. Which line changes?
  3. Use a loop and addYears(leapDay, n) to count how many years from 2025 to 2104 keep 29 February. Is 2100 in your list?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.