Calculate Age with Birth Date in Python

  • Share this:

Code introduction


This function calculates the age based on a given birth date.


Technology Stack : datetime, json

Code Type : Function

Code Difficulty : Intermediate


                
                    
import datetime
import json

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