用户输入身份证号码,输入用户的出生年月,并计算他现在多少岁了(算到2021-11-15)
>>> from datetime import *
>>> def str2date(Id):
date = Id[6:10],Id[10:12],Id[12:14]
return '-'.join(date)
>>> def calculate_age(date):
born = datetime.strptime(date, '%Y-%m-%d')
today = datetime.strptime('2021-11-15', '%Y-%m-%d')
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
>>> s = '320500200207081111'
>>> calculate_age(str2date(s))
19
>>> s = '320500200212081111'
>>> calculate_age(str2date(s))
18
>>>