addMonths method

DateTime addMonths(
  1. int months
)

Moves this date by months calendar months.

If the new month is too short, the day is clamped to its last day, so the 31st of January plus one month is the 28th (or 29th) of February. Use a negative number to go back.

Implementation

DateTime addMonths(int months) {
  final DateTime firstOfTarget = copyWith(month: month + months, day: 1);
  final int lastDay = firstOfTarget.daysInMonth;

  return firstOfTarget.copyWith(day: day > lastDay ? lastDay : day);
}