addMonths method
Adds the specified number of months to the current DateTime.
Calendar-month step: when the target month is shorter, the day clamps to
its last day (Jan 31 + 1 = Feb 28/29). The time of day (to the
microsecond) and the UTC/local kind are kept; negative months move
backward across year boundaries. Use this for a "calendar months" step;
test/datetime/add_months_calendar_contract_test.dart pins the contract.
Example:
DateTime(2024, 1, 31, 9).addMonths(1); // 2024-02-29 09:00
DateTime.utc(2024, 3, 31).addMonths(-1); // 2024-02-29 00:00Z
Args: months (int): The number of months to add.
Returns: DateTime: A new DateTime object with the added months. Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
DateTime addMonths(int months) {
if (months == 0) {
return this;
}
return Jiffy.parseFromDateTime(this).add(months: months).dateTime;
}