addMonths method

DateTime addMonths(
  1. int amount
)

Add a certain amount of months to this date and clamp overflow days.

Implementation

DateTime addMonths(int amount) {
  final totalMonths = year * 12 + month - 1 + amount;
  final targetYear = totalMonths ~/ 12;
  final targetMonth = totalMonths % 12 + 1;
  final maxDay = DateTime(targetYear, targetMonth + 1, 0).day;

  return _copyDateTime(
    this,
    year: targetYear,
    month: targetMonth,
    day: day > maxDay ? maxDay : day,
  );
}