年龄从python出生日期 [英] Age from birthdate in python

查看:218
本文介绍了年龄从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出生日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆