The addYears function adds a specified number of years to a given date, returning a new Date object. It handles leap year edge cases intelligently and validates all inputs before processing, making it a reliable choice for year-based date transformations in Chronia’s consistent API surface.
function addYears(date: DateInput, amount: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
amount |
number |
The number of years to add (can be negative to subtract years) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified years added, or Invalid Date if any input is invalid |
The addYears function calculates a new date by adding the specified number of years to the provided date. It preserves the month, day, and time components from the original date while intelligently handling leap year edge cases, particularly when the source date is February 29 and the target year is not a leap year.
Date when:date argument is a valid Date object or finite numeric timestampamount argument is a finite numberThe returned date will have:
amount (fractional amounts are truncated toward zero)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityamount argument is NaN, Infinity, or -InfinityMath.trunc (e.g., 1.9 becomes 1, -1.9 becomes -1)Date | number can be passed for the date parameteramount values to subtract yearsimport { addYears } from "chronia";
// Calculate 5-year contract renewal date
const contractStart = new Date(2020, 0, 15); // January 15, 2020
const renewalDate = addYears(contractStart, 5);
// Returns: Date object representing January 15, 2025
// Calculate past anniversary (negative years)
const currentDate = new Date(2025, 5, 10); // June 10, 2025
const threeYearsAgo = addYears(currentDate, -3);
// Returns: Date object representing June 10, 2022
import { addYears } from "chronia";
// Calculate when someone will turn 18
const birthDate = new Date(2010, 3, 22); // April 22, 2010
const adultDate = addYears(birthDate, 18);
// Returns: Date object representing April 22, 2028
// Handle leap year birthday
const leapBirthday = new Date(2004, 1, 29); // February 29, 2004
const nextBirthday = addYears(leapBirthday, 1);
// Returns: Date object representing February 28, 2005 (2005 is not a leap year)
const leapToLeap = addYears(leapBirthday, 4);
// Returns: Date object representing February 29, 2008 (2008 is a leap year)
import { addYears } from "chronia";
// Project quarterly report date 2 years forward
const quarterlyReport = new Date(2023, 2, 31); // March 31, 2023
const futureReport = addYears(quarterlyReport, 2);
// Returns: Date object representing March 31, 2025
// Fractional years are truncated
const baseDate = new Date(2020, 0, 1); // January 1, 2020
const result = addYears(baseDate, 2.9);
// Returns: Date object representing January 1, 2022 (2.9 truncated to 2)
// Negative fractional truncation
const negResult = addYears(baseDate, -1.7);
// Returns: Date object representing January 1, 2019 (-1.7 truncated to -1)
import { addYears } from "chronia";
// Invalid date input
const invalidDate = new Date("invalid string");
const result1 = addYears(invalidDate, 3);
// Returns: Invalid Date
// Invalid amount (NaN)
const validDate = new Date(2020, 0, 1);
const result2 = addYears(validDate, NaN);
// Returns: Invalid Date
// Invalid amount (Infinity)
const result3 = addYears(validDate, Infinity);
// Returns: Invalid Date
// Checking for invalid results
function safeAddYears(date: Date | number, years: number): Date | null {
const result = addYears(date, years);
return isNaN(result.getTime()) ? null : result;
}
safeAddYears(new Date(2020, 0, 1), 5); // Returns: Date object
safeAddYears(new Date("invalid"), 5); // Returns: null
import { addYears } from "chronia";
// Time components are preserved
const dateWithTime = new Date(2020, 5, 15, 14, 30, 45, 123);
// June 15, 2020, 14:30:45.123
const futureDate = addYears(dateWithTime, 3);
// Returns: Date object representing June 15, 2023, 14:30:45.123
// Hours, minutes, seconds, and milliseconds are preserved
// Using timestamp
const timestamp = new Date(2020, 0, 1, 12, 0, 0).getTime();
const fromTimestamp = addYears(timestamp, 2);
// Returns: Date object representing January 1, 2022, 12:00:00