Python Age Calculation from Birth Date

  • Share this:

Code introduction


Calculates the age of a person given their birth date


Technology Stack : datetime

Code Type : Function

Code Difficulty : Intermediate


                
                    
import datetime

def calculate_age(birth_date):
    today = datetime.date.today()
    age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
    return age                
              
Tags: