如何从今天的日期和人的出生日期中找到python的年龄?生日是来自Django模型中的DateField的。
考虑到int(True)为1且int(False)为0,可以轻松得多:
from datetime import date
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句