本文介绍了年龄从python出生日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从今天到今天,我怎么能找到一个python的年龄和一个人的出生日期?生日是来自Django模型中的DateField。
How can I find an age in python from today's date and a persons birthdate? The birthdate is a from a DateField in a Django model.
推荐答案
考虑到int(True)是1和int(False)为0:
That can be done much simpler considering that int(True) is 1 and int(False) is 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))
这篇关于年龄从python出生日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!