Dates and times · 10 / 15 Exercise Medium

Compute the age

Objective

Create a Python program that computes a person's age on a given date and how many days are left until their next birthday. The reference date is read from the input so that the result is always the same.

Instructions

  • Read an integer n and then n lines with two dates separated by a space: the birth date and the reference date (both YYYY-MM-DD). Nobody was born on 29 February.
  • Age: difference in years, subtracting 1 if on the reference date the birthday of that year has not yet been reached (compare the (month, day) tuples).
  • Next birthday: the birthday date in the reference year; if it has already passed, the one of the following year. If it is today, 0 days are left.
  • Show Age: a years, next birthday in d days.
Input
3
1990-05-20 2026-11-14
2000-11-14 2026-11-14
1985-12-31 2026-01-01
Output
Age: 36 years, next birthday in 187 days
Age: 26 years, next birthday in 0 days
Age: 40 years, next birthday in 364 days

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise