Python ProgrammingPython Programming

Find an age in python from today's date and a persons birthdate

from datetime import date


def calculate_age(born):
    today = date.today()
    try:
        birthday = born.replace(year=today.year)
    except ValueError:
        birthday = born.replace(year=today.year, month=born.month + 1, day=1)
    if birthday > today:
        return today.year - born.year - 1
    else:
        return today.year - born.year


print(calculate_age(date(2001, 3, 1)))
Output
20