How to add or subtract calendar months
Handle month-end dates without overflowing into the wrong month.
Months are not fixed durations
Adding one month is not the same as adding thirty days. The result should be computed by calendar fields under an explicit month-end rule.
The month-end problem
A target month may lack the original day number. Systems commonly clamp to the target month's final day, reject the input or preserve an end-of-month convention.
Document the rule
Billing, subscriptions and legal deadlines may use different policies. Tests should cover January 31, February, leap years and year boundaries.
Related month guides
- How many days are in each month? — A complete guide to 28-, 29-, 30- and 31-day months.
- How much of the current month has passed? — Calculate a live percentage for the current calendar month.
- Fiscal months and accounting periods — Why reporting months may not follow calendar boundaries.