手机
当前位置:查字典教程网 >脚本专栏 >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中的yield使用方法

python中精确输出JSON浮点数的方法

python中使用sys模板和logging模块获取行号和函数名的方法

Python GAE、Django导出Excel的方法

Python中使用中文的方法

Python获取脚本所在目录的正确方法

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

利用python获得时间的实例说明

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

精品推荐
分类导航