difference method

Period difference(
  1. Moment other
)

Returns the absolute difference between this Moment and other as a Period.

The result is always positive regardless of which moment comes first.

final gap = Moment.now().difference(birthday);
print(gap); // e.g. "26 years, 2 months, ..."

Implementation

Period difference(Moment other) {
  int totalSecs = totalSeconds - other.totalSeconds;
  totalSecs = totalSecs.abs();

  int secs = totalSecs % 60;
  int totalMins = totalSecs ~/ 60;
  int mins = totalMins % 60;
  int totalHrs = totalMins ~/ 60;
  int hrs = totalHrs % 24;
  int days = totalHrs ~/ 24;

  int yrs = days ~/ 365;
  int remainingDays = days % 365;
  int months = remainingDays ~/ 30;
  remainingDays = remainingDays % 30;

  return Period(
    years: yrs,
    months: months,
    days: remainingDays,
    hours: hrs,
    minutes: mins,
    seconds: secs,
  );
}