python根据出生日期获得年龄的方法,python出生日期
python根据出生日期获得年龄的方法,python出生日期
本文实例讲述了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程序设计有所帮助。
相关内容
- 使用Python3中的gettext模块翻译Python源码以支持多语言,
- python计算N天之后日期的方法,python计算n日期
- 详解Python中内置的NotImplemented类型的用法,python开发技
- 使用Python实现一个简单的项目监控,python项目监控
- 通过代码实例展示Python中列表生成式的用法,python生成
- Python实现国外赌场热门游戏Craps(双骰子),pythoncrap
- 用Python的pandas框架操作Excel文件中的数据教程,
- Python2.x中str与unicode相关问题的解决方法,python2.xunico
- Python中分数的相关使用教程,python使用教程
- 优化Python代码使其加快作用域内的查找,python域内
评论关闭