addYears method

DateTime addYears(
  1. int years
)

Adds the specified number of years to the current DateTime.

Note: When adding a year to February 29th, it returns February 28th (not March 1st)

Args: years (int): The number of years to add.

Returns: DateTime: A new DateTime object with the added years.

Implementation

DateTime addYears(int years) {
  if (years == 0) {
    // same
    return this;
  }

  // We used the Jiffy package to add years, months, etc
  // https://stackoverflow.com/questions/54792056/add-subtract-months-years-to-date-in-dart
  return Jiffy.parseFromDateTime(this).add(years: years).dateTime;
}