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中统计函数运行耗时的方法
May 05 Python
小米5s微信跳一跳小程序python源码
Jan 08 Python
Python即时网络爬虫项目启动说明详解
Feb 23 Python
python unittest实现api自动化测试
Apr 04 Python
Python读取数据集并消除数据中的空行方法
Jul 12 Python
浅谈Python3中strip()、lstrip()、rstrip()用法详解
Apr 29 Python
python图像和办公文档处理总结
May 28 Python
实现Python与STM32通信方式
Dec 18 Python
django 读取图片到页面实例
Mar 27 Python
python实现简单坦克大战
Mar 27 Python
对python pandas中 inplace 参数的理解
Jun 27 Python
Python OpenCV形态学运算示例详解
Apr 07 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
PHP MYSQL乱码问题,使用SET NAMES utf8校正
2009/11/30 PHP
用php解析html的实现代码
2011/08/08 PHP
php数组函数序列之array_pop() - 删除数组中的最后一个元素
2011/11/07 PHP
PHP异步调用socket实现代码
2012/01/12 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(三)
2014/06/23 PHP
php导入excel文件到mysql数据库的方法
2015/01/14 PHP
PHP数组相加操作及与array_merge的区别浅析
2016/11/26 PHP
JQuery 1.4 中的Ajax问题
2010/01/23 Javascript
Array.prototype.slice 使用扩展
2010/06/09 Javascript
JQuery select控件的相关操作实现代码
2012/09/14 Javascript
jquery判断元素是否隐藏的多种方法
2014/05/06 Javascript
javascript 数组操作详解
2015/01/29 Javascript
关于微信中a链接无法跳转问题
2016/08/02 Javascript
Javascript中作用域的详细介绍
2016/10/06 Javascript
搭建简单的nodejs http服务器详解
2017/03/09 NodeJs
怎样在vue项目下添加ESLint的方法
2019/05/16 Javascript
JS实现鼠标移动拖尾
2020/12/27 Javascript
[05:46]DOTA2英雄梦之声_第18期_陈
2014/06/20 DOTA
介绍Python中内置的itertools模块
2015/04/29 Python
Python批量转换文件编码格式
2015/05/17 Python
python操作redis的方法
2015/07/07 Python
Python实现快速傅里叶变换的方法(FFT)
2018/07/21 Python
pandas的resample重采样的使用
2020/04/24 Python
Python控制台实现交互式环境执行
2020/06/09 Python
对python中list的五种查找方法说明
2020/07/13 Python
Python在字符串中处理html和xml的方法
2020/07/31 Python
HTML5 移动页面自适应手机屏幕四类方法总结
2017/08/17 HTML / CSS
师范生实习自我鉴定
2013/11/01 职场文书
学雷锋活动总结范文
2014/04/25 职场文书
优秀大学生自荐信
2014/06/09 职场文书
英语三分钟演讲稿
2014/08/19 职场文书
邓小平文选读书笔记
2015/06/29 职场文书
jquery插件实现搜索历史
2021/04/24 jQuery
详解解Django 多对多表关系的三种创建方式
2021/08/23 Python
python中super()函数的理解与基本使用
2021/08/30 Python
Golang实现可重入锁的示例代码
2022/05/25 Golang