tkalve tkalve - 1 year ago 155
Python Question

Age from birthdate in python

How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.

Answer Source

That can be done much simpler considering that int(True) is 1 and int(False) is 0:

from datetime import date

def calculate_age(born):
    today =
    years_difference = today.year - born.year
    is_before_birthday = (today.month, < (born.month,
    elapsed_years = years_difference - int(is_before_birthday)
    return elapsed_years
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download