addYears()
Adds or subtracts years from a Date object. Handles leap years and edge cases appropriately.
Syntax
addYears(dateObj, years[, timeZone])Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
dateObj | Date | Yes | The base Date object |
years | number | Yes | Number of years to add (positive) or subtract (negative) |
timeZone | TimeZone | string | No | Timezone for the calculation |
Returns
Date - A new Date object with the specified number of years added or subtracted
Basic Examples
Adding and Subtracting Years
import { addYears } from 'date-and-time';
const date = new Date(2024, 0, 15); // January 15, 2024
// Add yearsconst future = addYears(date, 3);console.log(future); // January 15, 2027
// Subtract yearsconst past = addYears(date, -2);console.log(past); // January 15, 2022Daylight Saving Time Aware Calculations
import { addYears } from 'date-and-time';
// Working with specific timezonesconst nyDate = new Date('2024-03-10T05:00:00Z'); // March 10, 2024 05:00 UTC (DST transition day)
// Add years in New York timezoneconst futureNY = addYears(nyDate, 1, 'America/New_York');console.log(futureNY); // March 10, 2025 04:00 UTC (EST, DST adjusted)
// UTC calculation for comparisonconst futureUTC = addYears(nyDate, 1, 'UTC');console.log(futureUTC); // March 10, 2025 05:00 UTC (same time, no DST adjustment)Use Cases
Age Calculation
function calculateAge(birthDate: Date): number { const now = new Date(); const thisYear = addYears(birthDate, now.getFullYear() - birthDate.getFullYear());
if (thisYear > now) { return now.getFullYear() - birthDate.getFullYear() - 1; } return now.getFullYear() - birthDate.getFullYear();}
const birthDate = new Date(1990, 6, 15); // July 15, 1990;console.log(calculateAge(birthDate)); // Current ageFinancial Year Calculations
function getFinancialYearEnd(date: Date): Date { const currentYear = date.getFullYear(); const financialYearEnd = new Date(currentYear, 2, 31); // March 31
if (date <= financialYearEnd) { return financialYearEnd; } else { return addYears(financialYearEnd, 1); // Next year's March 31 }}
const someDate = new Date(2024, 5, 15); // June 15, 2024console.log(getFinancialYearEnd(someDate)); // March 31, 2025Edge Cases and Behavior
February 29 Handling
// Starting from February 29 (leap year)const feb29 = new Date(2024, 1, 29); // February 29, 2024
// Adding to non-leap yearsconsole.log(addYears(feb29, 1)); // February 28, 2025console.log(addYears(feb29, 2)); // February 28, 2026console.log(addYears(feb29, 3)); // February 28, 2027console.log(addYears(feb29, 4)); // February 29, 2028 (leap year)Negative Years
const date = new Date(2024, 6, 15); // July 15, 2024
// Go back in timeconsole.log(addYears(date, -10)); // July 15, 2014console.log(addYears(date, -100)); // July 15, 1924Immutability
addYears() does not modify the original Date object:
const originalDate = new Date(2024, 0, 15);const modifiedDate = addYears(originalDate, 5);
console.log(originalDate); // January 15, 2024 (unchanged)console.log(modifiedDate); // January 15, 2029 (new object)See Also
addMonths()- Add/subtract monthsaddDays()- Add/subtract daysaddHours()- Add/subtract hoursaddMinutes()- Add/subtract minutesaddSeconds()- Add/subtract secondsaddMilliseconds()- Add/subtract millisecondssubtract()- Calculate differences with Duration objects