python根據(jù)出生日期返回年齡的方法
本文實(shí)例講述了python根據(jù)出生日期返回年齡的方法。分享給大家供大家參考。具體實(shí)現(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')
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- 利用python-docx模塊寫(xiě)批量生日邀請(qǐng)函
- 在Python dataframe中出生日期轉(zhuǎn)化為年齡的實(shí)現(xiàn)方法
- python顯示生日是星期幾的方法
- python根據(jù)出生日期獲得年齡的方法
- Python 將 QQ 好友頭像生成祝福語(yǔ)的實(shí)現(xiàn)代碼
- python通過(guò)安裝itchat包實(shí)現(xiàn)微信自動(dòng)回復(fù)收到的春節(jié)祝福
- python實(shí)現(xiàn)彈窗祝福效果
- python實(shí)現(xiàn)祝福彈窗效果
- 教你怎么用Python實(shí)現(xiàn)自動(dòng)生日祝福
相關(guān)文章
Python turtle庫(kù)繪制菱形的3種方式小結(jié)
今天小編就為大家分享一篇Python turtle庫(kù)繪制菱形的3種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11PyCharm2020.1.1與Python3.7.7的安裝教程圖文詳解
這篇文章主要介紹了PyCharm2020.1.1與Python3.7.7的安裝教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Pandas.DataFrame重置列的行名實(shí)現(xiàn)(set_index)
本文主要介紹了Pandas.DataFrame重置列的行名實(shí)現(xiàn)(set_index),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02python使用pip成功導(dǎo)入庫(kù)后還是報(bào)錯(cuò)的解決方法(針對(duì)vscode)
最近在學(xué)爬蟲(chóng),但在使用Scrapy包時(shí),在終端通過(guò)pip裝好包以后,在pycharm中導(dǎo)入包時(shí),依然會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于python使用pip成功導(dǎo)入庫(kù)后還是報(bào)錯(cuò)的解決方法,需要的朋友可以參考下2022-07-07一篇文章帶你了解Python的進(jìn)程,線程和協(xié)程
這篇文章主要為大家介紹了Python的進(jìn)程,線程和協(xié)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-01-01Python函數(shù)的參數(shù)常見(jiàn)分類與用法實(shí)例詳解
這篇文章主要介紹了Python函數(shù)的參數(shù)常見(jiàn)分類與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Python函數(shù)的形參、實(shí)參、默認(rèn)參數(shù)、可變參數(shù)等概念、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03