Pythonで誕生日計算


def calc_age(birthdate):
    now=datetime.datetime.now()
    bdate=datetime.datetime.strptime(birthdate,"%Y-%m-%d")
    age=now.year-bdate.year
    if now.timetuple().tm_yday < bdate.timetuple().tm_yday:
        age-=1
    return age

>>> calc_age("2001-11-18")
15
>>> calc_age("2001-10-18")
16