getMonthDifference method

int getMonthDifference(
  1. DateTime date
)

Gets difference of months between date and calling object.

Implementation

int getMonthDifference(DateTime date) {
  if (year == date.year) return ((date.month - month).abs() + 1);

  var months = ((date.year - year).abs() - 1) * 12;

  if (date.year >= year) {
    months += date.month + (13 - month);
  } else {
    months += month + (13 - date.month);
  }

  return months;
}