国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python根據出生日期返回年齡的方法

2020-02-23 00:26:16
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新巴尔虎右旗| 固安县| 新民市| 登封市| 扬州市| 正阳县| 皋兰县| 鄂尔多斯市| 运城市| 腾冲县| 千阳县| 鄂托克前旗| 玉田县| 达尔| 锦州市| 普安县| 乌兰察布市| 逊克县| 交城县| 锡林浩特市| 清水县| 繁昌县| 玛多县| 阜平县| 三穗县| 托里县| 巫溪县| 深泽县| 黎川县| 布尔津县| 沂南县| 安康市| 河津市| 湛江市| 砚山县| 乌恰县| 兴化市| 秀山| 鞍山市| 论坛| 澄城县|