DocsSintaxDatetimeadd_months

add_months

Shifts a date by a whole number of calendar months, forward on a positive amount and back on a negative one.

It earns a name of its own because a renewal, a billing period and a notice period are all written in months, and a month is not a fixed number of days, so a template that said add_days:30 would drift a day or two every time it ran.

A day that the target month does not have lands on that month's last day, so the 31st of January plus one month is the 28th of February (the 29th in a leap year) rather than the 2nd or 3rd of March. Overflowing into the next month is the arithmetic answer, and it is wrong for every case that asks the question, since a subscription that charges on the 31st charges in February.

The clock time and the zone come through untouched.

InputArgumentsReturnstime.Timeinttime.Time
Source
BasicSource

Clamps a day the target month does not have, so a subscription charging on the 31st charges on the last day of February.

Given

{"charged_on": time.Date(2024, 1, 31, 0, 0, 0, 0, time.UTC)}

Template

{{ charged_on | add_months:1 | date:'Y-m-d' }}

Result

2024-02-29