1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from datetime import date def calculate_age(birthday): today = date.today() born = birthday try: birthday = born.replace(year=today.year) except ValueError: birthday = born.replace(year=today.year, month=born.month + 1, day=1) age = today.year - born.year if birthday > today: age -= 1 return age