plusPeriod method
Adds Period of time.
Increments (or decrements) the date by a specific number of months
or years while—as much as possible—keeping the day the same. When this
is not possible the result will be the last day of the month. For
example, adding one month to 2023-01-31
gives 2023-01-28
.
The days part is applied last. For example, adding one month and one day
to 2023-01-31
first adds one month to get 2023-02-28
and then
adds one day for a final result of 2023-03-01
.
Implementation
@override
LocalDate plusPeriod(Period p) {
var y = year + p.years + p.months ~/ 12;
var months = p.months.remainder(12);
var m = month + months;
if (m < 1) {
--y;
} else if (m > 12) {
++y;
}
m = (m - 1) % 12 + 1;
return LocalDate(y, m, min(day, daysInMonth(y, m)))
.plusTimespan(Timespan(days: p.days));
}