The base date as a Date object or timestamp (number)
The number of years to add (can be negative to subtract)
A new Date object with the years added, or Invalid Date if any input is invalid
// Add positive years
const result = addYears(new Date(2020, 0, 15), 3);
// Returns: 2023-01-15
// Subtract years (negative amount)
const result = addYears(new Date(2020, 0, 15), -3);
// Returns: 2017-01-15
// Fractional amounts are truncated
const result = addYears(new Date(2020, 0, 1), 1.9);
// Returns: 2021-01-01 (1.9 truncated to 1)
// Leap year to non-leap year (Feb 29 → Feb 28)
const result = addYears(new Date(2020, 1, 29), 1);
// Returns: 2021-02-28 (2021 is not a leap year)
// Leap year to leap year (Feb 29 → Feb 29)
const result = addYears(new Date(2020, 1, 29), 4);
// Returns: 2024-02-29 (2024 is a leap year)
// Invalid inputs return Invalid Date
const result = addYears(new Date("invalid"), 3);
// Returns: Invalid Date
Add the specified number of years to the given date.
This function validates arguments before processing and returns a new Date instance with the specified number of years added. Fractional years are truncated toward zero. Preserves month, day, and time components. When the source date is Feb 29 and the target year is not a leap year, the result becomes Feb 28.