calculateGregorianAge static method

Map<String, int> calculateGregorianAge({
  1. required int birthYear,
  2. required int birthMonth,
  3. required int birthDay,
})

Calculate age from Gregorian birthdate Returns Map with years, months, and days

Implementation

static Map<String, int> calculateGregorianAge({
  required int birthYear,
  required int birthMonth,
  required int birthDay,
}) {
  final today = DateTime.now();

  int years = today.year - birthYear;
  int months = today.month - birthMonth;
  int days = today.day - birthDay;

  /// Adjust for negative days
  if (days < 0) {
    months--;
    // Get days in previous month
    final prevMonth = DateTime(today.year, today.month - 1, 1);
    final daysInPrevMonth = DateTime(prevMonth.year, prevMonth.month + 1, 0).day;
    days += daysInPrevMonth;
  }

  /// Adjust for negative months
  if (months < 0) {
    years--;
    months += 12;
  }

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