gpt4 book ai didi

python - python中从生日算起的年龄

转载 作者:IT老高 更新时间:2023-10-28 12:26:45 31 4
gpt4 key购买 nike

如何从今天的日期和一个人的生日在 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))

关于python - python中从生日算起的年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2217488/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com