Python - 從生日計算年齡

發佈日期:2021-5-27
import datetime

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


birth = datetime.date(1990, 1, 1)
age = calculate_age(birth)
print(age)

資料來源