calculateDetailedAge static method

Map<String, int> calculateDetailedAge(
  1. HijriDate birthDate, [
  2. HijriDate? currentDate
])

Calculates detailed age (years, months, days)

Implementation

static Map<String, int> calculateDetailedAge(HijriDate birthDate,
    [HijriDate? currentDate]) {
  currentDate ??= HijriDate.now();

  int years = currentDate.hYear - birthDate.hYear;
  int months = currentDate.hMonth - birthDate.hMonth;
  int days = currentDate.hDay - birthDate.hDay;

  if (days < 0) {
    months--;
    final daysInPrevMonth = currentDate.subtractMonths(1).getDaysInMonth(
        currentDate.hYear,
        currentDate.hMonth - 1 <= 0 ? 12 : currentDate.hMonth - 1);
    days += daysInPrevMonth;
  }

  if (months < 0) {
    years--;
    months += 12;
  }

  return {
    'years': years,
    'months': months,
    'days': days,
  };
}