addMonths method

DateTime addMonths(
  1. int count
)

This is to simulate C# behaviour. We guarantee the month will change to neighbour value and the day value is valid for that month.

Implementation

DateTime addMonths(int count) {
  int targetMonth = (month + count) % 12;
  if (targetMonth == 0) {
    targetMonth = 12;
  }
  DateTime result = DateTime(year, month + count, day, hour, minute, second, millisecond, microsecond);
  while (result.month != targetMonth) {
    result = result.subtract(const Duration(days: 1));
  }
  return result;
}