import datetime
def calculate_age(birth_s='20181215'):
birth_d = datetime.datetime.strptime(birth_s, "%Y%m%d")
today_d = datetime.datetime.now()
birth_t = birth_d.replace(year=today_d.year)
if today_d > birth_t:
age = today_d.year - birth_d.year
else:
age = today_d.year - birth_d.year - 1
print('出生日期:%s' % birth_d)
print('今年生日:%s' % birth_t)
print('今天日期:%s' % today_d)
print('如果今天日期大于今年生日,今年-出生年=年龄')
print('如果今天日期不大于今年生日,今年-出生年-1=年龄')
print('年龄:%s' % age)
return age
if __name__ == '__main__':
print(calculate_age('20171216'))
https://www.cnblogs.com/jackadam/p/12041838.html