age method

int age({
  1. DateTime? now,
})

Calculates the number of full years between this date and now.

This is the typical "age" calculation: how many birthdays have passed between this date and the reference date.

now defaults to the current date (from DateTime.now).

final birthDate = DateTime(2000, 1, 15);
// If today is 2025-06-01: age → 25

Implementation

int age({DateTime? now}) {
  now ??= DateTime.now();
  final age = now.year - year;
  // Adjust if the birthday hasn't occurred yet this year.
  if (now.month < month || (now.month == month && now.day < day)) {
    return age - 1;
  }
  return age;
}