手机
当前位置:查字典教程网 >脚本专栏 >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根据出生日期获得年龄的方法】相关文章:

让python的Cookie.py模块支持冒号做key的方法

tornado捕获和处理404错误的方法

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

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

python操作日期和时间的方法

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

Python中使用中文的方法

Python GAE、Django导出Excel的方法

python处理文本文件实现生成指定格式文件的方法

python解析json实例方法

精品推荐
分类导航