Exact Age in Years

Medium ⏱ 12 min 62% acceptance ★★★★★ 4.7
Write age_years(born, today) returning a person's completed age in years — the subtle part being the birthday check: subtract one if this year's birthday hasn't happened yet. Comparing (month, day) tuples handles it cleanly.

Examples

Example 1
Input
age_years(date(2000, 8, 15), date(2026, 7, 28))
Output
25
Explanation

The August birthday has not arrived by July 28.

Constraints

  • No external libraries (no dateutil).
  • Handle the pre-birthday case.

Topics

Date & Timedate arithmetic

Companies

HDFC BankPolicyBazaarLIC Tech

Hints

Hint 1

Start with today.year - born.year.

Hint 2

Subtract 1 when (today.month, today.day) < (born.month, born.day).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.