Age calculator from date of birth in every time unit using Python.

DESCRIPTION:

Python provides datetime module to deal with all datetime related issues in python. Using datetime we can find the age by subtracting birth year from current year. Along with this, we need to focus on the birth month and birthday. For this, we check if current month and date are less than birth month and date. If yes subtract 1 from age, otherwise 0.

CODE:

def calculateAge(birthDate):
    today = date.today()
    age = today.year - birthDate.year - ((today.month, today.day) < (birthDate.month, birthDate.day))
    return age  
    print(calculateAge(date(1997, 2, 3)), "years")

 

OUTPUT:

21 years

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top