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

首頁 > 編程 > Python > 正文

python根據(jù)出生日期返回年齡的方法

2019-11-25 17:54:25
字體:
來源:轉載
供稿:網友

本文實例講述了python根據(jù)出生日期返回年齡的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

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程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 秦皇岛市| 宁安市| 诸暨市| 阿拉尔市| 深泽县| 昌图县| 隆回县| 南木林县| 安图县| 汾西县| 长治市| 元朗区| 抚宁县| 蓬安县| 潮安县| 弋阳县| 阳原县| 屯留县| 右玉县| 繁峙县| 廊坊市| 天镇县| 石狮市| 临城县| 盐山县| 南丹县| 田东县| 兰溪市| 红原县| 桓台县| 务川| 临高县| 昌邑市| 镇巴县| 周宁县| 米泉市| 台北县| 梁平县| 类乌齐县| 华亭县| 文昌市|