python根据出生日期返回年龄的方法


Posted in Python onMarch 26, 2015

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

def CalculateAge(self, Date):
    '''Calculates the age and days until next birthday from the given birth date'''
    try:
      Date = Date.split('.')
      BirthDate = datetime.date(int(Date[0]), int(Date[1]), int(Date[2]))
      Today = datetime.date.today()
      if (Today.month > BirthDate.month):
        NextYear = datetime.date(Today.year + 1, BirthDate.month, BirthDate.day)
      elif (Today.month < BirthDate.month):
        NextYear = datetime.date(Today.year, Today.month + (BirthDate.month - Today.month), BirthDate.day)
      elif (Today.month == BirthDate.month):
        if (Today.day > BirthDate.day):
          NextYear = datetime.date(Today.year + 1, BirthDate.month, BirthDate.day)
        elif (Today.day < BirthDate.day):
          NextYear = datetime.date(Today.year, BirthDate.month, Today.day + (BirthDate.day - Today.day))
        elif (Today.day == BirthDate.day):
          NextYear = 0
      Age = Today.year - BirthDate.year
      if NextYear == 0: #if today is the birthday
        return '%d, days until %d: %d' % (Age, Age+1, 0)
      else:
        DaysLeft = NextYear - Today
        return '%d, days until %d: %d' % (Age, Age+1, DaysLeft.days)
    except:
      return 'Wrong date format'

使用方法如下:

print CheckDate('2000.05.05')

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

Python 相关文章推荐
python求素数示例分享
Feb 16 Python
Python实现一个简单的MySQL类
Jan 07 Python
Python利用Nagios增加微信报警通知的功能
Feb 18 Python
Python计算两个日期相差天数的方法示例
May 23 Python
深入浅析python 中的匿名函数
May 21 Python
python 爬虫一键爬取 淘宝天猫宝贝页面主图颜色图和详情图的教程
May 22 Python
详解Python if-elif-else知识点
Jun 11 Python
浅谈python之高阶函数和匿名函数
Mar 21 Python
python中pytest收集用例规则与运行指定用例详解
Jun 27 Python
关于django 1.10 CSRF验证失败的解决方法
Aug 31 Python
使用Pandas的Series方法绘制图像教程
Dec 04 Python
如何使用python切换hosts文件
Apr 29 Python
python获取远程图片大小和尺寸的方法
Mar 26 #Python
python使用cStringIO实现临时内存文件访问的方法
Mar 26 #Python
python使用pil生成缩略图的方法
Mar 26 #Python
python实现基于两张图片生成圆角图标效果的方法
Mar 26 #Python
python正则表达式match和search用法实例
Mar 26 #Python
python根据开头和结尾字符串获取中间字符串的方法
Mar 26 #Python
pymongo实现控制mongodb中数字字段做加法的方法
Mar 26 #Python
You might like
SONY SRF-22W(33W)的电路分析和维修案例
2021/03/02 无线电
PHP完整的日历类(CLASS)
2006/11/27 PHP
一个经典实用的PHP图像处理类分享
2014/11/18 PHP
php删除左端与右端空格的方法
2014/11/29 PHP
PHP中开启gzip压缩的2种方法
2015/01/31 PHP
php lcg_value与mt_rand生成0~1随机小数的效果对比分析
2017/04/05 PHP
PHP设计模式之工厂模式定义与用法详解
2018/04/03 PHP
jquery中获得$.ajax()事件返回的值并添加事件的方法
2010/04/15 Javascript
js自定义事件代码说明
2011/01/31 Javascript
原始的js代码和jquery对比体会
2013/09/10 Javascript
JQuery选择器绑定事件及修改内容的方法
2015/01/23 Javascript
jquery动态导航插件dynamicNav用法实例分析
2015/09/06 Javascript
JavaScript的removeChild()函数用法详解
2015/12/27 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
2016/11/30 Javascript
详解windows下vue-cli及webpack 构建网站(三)使用组件
2017/06/17 Javascript
js核心基础之构造函数constructor用法实例分析
2019/05/11 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
[00:37]食人魔魔法师轮盘吉兆顺应全新至宝将拥有额外款式
2019/12/19 DOTA
Python3使用requests发闪存的方法
2016/05/11 Python
详解Python3中字符串中的数字提取方法
2017/01/14 Python
Python只用40行代码编写的计算器实例
2017/05/10 Python
pytorch打印网络结构的实例
2019/08/19 Python
浅析Python 条件控制语句
2020/07/15 Python
Python Pygame实现俄罗斯方块
2021/02/19 Python
解决pytorch 的state_dict()拷贝问题
2021/03/03 Python
白俄罗斯在线大型超市:e-dostavka.by
2019/07/25 全球购物
店长职务说明书
2014/02/04 职场文书
活动总结书
2014/05/08 职场文书
2014年网管工作总结
2014/12/11 职场文书
高中教师个人总结
2015/02/10 职场文书
装修公司工程部经理岗位职责
2015/04/09 职场文书
2015年小学校长工作总结
2015/05/19 职场文书
2015年国庆节广播稿
2015/08/19 职场文书
html5移动端禁止长按图片保存的实现
2021/04/20 HTML / CSS
教你用Python matplotlib库制作简单的动画
2021/06/11 Python
服务器nginx权限被拒绝解决案例
2022/09/23 Servers