Skip to main content Link Search Menu Expand Document (external link)

addYears() : Add Years to Date


On this page

Summary

Add years to a date. Use negative values to subtract years.

Signature

addYears(date, years)

Parameters

Parameter Type Required Description
date DateTime Yes The date to modify
years Number Yes Number of years to add (can be negative)

Returns

Type: DateTime

A new DateTime with the specified years added.


Examples

Add Years

<p>Expiry: </p>

Data:

doc.Params["model"] = new {
    issued = new DateTime(2024, 3, 15)
};

Output:

<p>Expiry: March 15, 2029</p>

Calculate Age Next Year

<p>Next year you'll be  years old</p>

Contract Renewal Dates

<h3>Renewal Schedule</h3>

  <p>Year : </p>

Data:

doc.Params["model"] = new {
    startDate = new DateTime(2024, 1, 1),
    renewalYears = new[] { 1, 2, 3, 5, 10 }
};

Output:

<h3>Renewal Schedule</h3>
<p>Year 1: 2025-01-01</p>
<p>Year 2: 2026-01-01</p>
<p>Year 3: 2027-01-01</p>
<p>Year 5: 2029-01-01</p>
<p>Year 10: 2034-01-01</p>

Subtract Years

<p>5 years ago: </p>

Notes

  • Input date is not modified (returns new date)
  • Can add positive or negative years
  • Handles leap years correctly
  • If Feb 29 on leap year + 1 year = Feb 28
  • Use addMonths() for month increments
  • Use addDays() for day increments

See Also