addOrSubtractYears method
Adds or subtracts the specified number of years from this DateTime. Using a positive value (e.g., 1) adds years, while a negative value (e.g., -1) subtracts years.
Implementation
DateTime addOrSubtractYears(int years) {
final newYear = year + years;
final newMonth = month;
var newDay = day;
// Adjust the day if it exceeds the number of days in the new month
while (newDay > DateTime(newYear, newMonth + 1, 0).day) {
newDay--;
}
return DateTime(
newYear,
newMonth,
newDay,
hour,
minute,
second,
millisecond,
microsecond,
);
}