手机
当前位置:查字典教程网 >脚本专栏 >python >python根据出生日期获得年龄的方法
python根据出生日期获得年龄的方法
摘要:本文实例讲述了python根据出生日期获得年龄的方法。分享给大家供大家参考。具体如下:这段代码可以根据用户的出生日期获得其年龄,born参数...

本文实例讲述了python根据出生日期获得年龄的方法。分享给大家供大家参考。具体如下:

这段代码可以根据用户的出生日期获得其年龄,born参数为date类型

def calculate_age(born): today = date.today() try: birthday = born.replace(year=today.year) except ValueError: # raised when birth date is February 29 # and the current year is not a leap year birthday = born.replace(year=today.year, day=born.day-1) if birthday > today: return today.year - born.year - 1 else: return today.year - born.year

希望本文所述对大家的Python程序设计有所帮助。

【python根据出生日期获得年龄的方法】相关文章:

wxpython 最小化到托盘与欢迎图片的实现方法

python操作日期和时间的方法

用Python脚本生成Android SALT扰码的方法

Python中使用中文的方法

python实现问号表达式(?)的方法

Python编写检测数据库SA用户的方法

python动态监控日志内容的示例

python生成指定长度的随机数密码

Python GAE、Django导出Excel的方法

python模拟登陆阿里妈妈生成商品推广链接

精品推荐
分类导航