Age Calculation from Birthdate

  • Share this:

Code introduction


Calculates the age based on the given birthdate.


Technology Stack : datetime, time, calendar

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import math
import os
import re
import sys
import time
import calendar

def calculate_age(birthdate):
    """
    计算给定日期的年龄。
    """
    today = time.time()
    birthdate_timestamp = calendar.timegm(time.localtime(birthdate))
    age = int((today - birthdate_timestamp) / 31536000)
    return age