calculateAgeAtDeath static method

int? calculateAgeAtDeath({
  1. required DateTime? dob,
  2. required DateTime? dod,
})

Calculates the age at death based on the date of birth (DOB) and date of death (DOD).

Args: dob (DateTime?): The date of birth. dod (DateTime?): The date of death.

Returns: int?: The calculated age at death, or null if either dob or dod is null, or if dod is before dob.

Implementation

static int? calculateAgeAtDeath({required DateTime? dob, required DateTime? dod}) {
  // Check if either dob or dod is null
  if (dob == null || dod == null) {
    return null;
  }

  // Check if dod is before dob
  if (dod.isBefore(dob)) {
    return null;
  }

  int age = dod.year - dob.year;

  // Adjust age if the birthday hasn't occurred yet in the dod year
  if (dod.month < dob.month || (dod.month == dob.month && dod.day < dob.day)) {
    age--;
  }

  return age;
}