addMonths static method
Returns a date with the specified months added or subtracted to the specified month.
If toMonth
is not specified, the current month is taken.
Implementation
static DateTime addMonths(int months, {DateTime? toMonth}) {
DateTime firstDayOfCurrentMonth = toMonth ?? DateTime.now();
if (months > 0) {
for (int i = 0; i < months; i++) {
firstDayOfCurrentMonth =
getLastDayOfMonth(month: firstDayOfCurrentMonth)
.add(const Duration(days: 1));
}
} else {
for (int i = 0; i > months; i--) {
firstDayOfCurrentMonth =
getFirstDayOfMonth(month: firstDayOfCurrentMonth)
.subtract(const Duration(days: 1));
}
}
return firstDayOfCurrentMonth;
}