addMonths method
- int months
Returns the date months months later; negative values go back.
The day is clamped to the length of the target month, so 1403/06/31 plus one month is 1403/07/30. This differs from DateTime, which rolls over into the next month. The result is date-only. Throws ArgumentError when the result is outside the supported range.
Implementation
PersianDate addMonths(int months) {
final total = year * 12 + (month - 1) + months;
// Dart's % is never negative, so this floors for negative totals too.
final targetYear = (total - total % 12) ~/ 12;
final targetMonth = total % 12 + 1;
if (targetYear < -61 || targetYear > 3177) {
throw ArgumentError.value(months, 'months', 'Result is out of range');
}
final length = jalaliMonthLength(targetYear, targetMonth);
return PersianDate(targetYear, targetMonth, day > length ? length : day);
}