addMonths method
Adds months to the date.
Example:
DateTime(2024, 1, 15).addMonths(2);
// DateTime(2024, 3, 15)
Implementation
DateTime addMonths(int months) {
var newMonth = month + months;
var newYear = year;
while (newMonth > 12) {
newMonth -= 12;
newYear++;
}
while (newMonth < 1) {
newMonth += 12;
newYear--;
}
final daysInMonth = DateTime(newYear, newMonth + 1, 0).day;
final newDay = day > daysInMonth ? daysInMonth : day;
return DateTime(newYear, newMonth, newDay, hour, minute, second, millisecond);
}