Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

addMonths() : Add Months to Date

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


On this page

Signature

addMonths(date, months)

Parameters

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

Returns

Type: DateTime

A new DateTime with the specified months added.


Examples

Add Months

<p>Due Date: {{format(addMonths(model.startDate, 3), 'MMMM dd, yyyy')}}</p>

Data:

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

Output:

<p>Due Date: June 15, 2024</p>

Subtract Months

<p>3 months ago: {{format(addMonths(model.today, -3), 'yyyy-MM-dd')}}</p>

Data:

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

Output:

<p>3 months ago: 2023-12-15</p>

Payment Schedule

{{#each model.payments}}
  <li>Payment {{add(@index, 1)}}: {{format(addMonths(model.startDate, @index), 'MMM dd, yyyy')}}</li>
{{/each}}

Data:

doc.Params["model"] = new {
    startDate = new DateTime(2024, 1, 15),
    payments = new[] { 1, 2, 3, 4, 5, 6 }
};

Output:

<li>Payment 1: Jan 15, 2024</li>
<li>Payment 2: Feb 15, 2024</li>
<li>Payment 3: Mar 15, 2024</li>
<li>Payment 4: Apr 15, 2024</li>
<li>Payment 5: May 15, 2024</li>
<li>Payment 6: Jun 15, 2024</li>

Annual Review Date

<p>Next Review: {{format(addMonths(model.lastReview, 12), 'MMMM dd, yyyy')}}</p>

Notes

  • Input date is not modified (returns new date)
  • Can add positive or negative months
  • Handles year boundaries automatically
  • If resulting day doesn’t exist (e.g., Feb 31), adjusts to last valid day
  • Use addYears() for year increments
  • Use addDays() for day increments

See Also