Python利用zhdate模塊實(shí)現(xiàn)農(nóng)歷日期處理
簡介
zhdate模塊統(tǒng)計從1900年到2100年的農(nóng)歷月份數(shù)據(jù)代碼,支持農(nóng)歷和公歷之間的轉(zhuǎn)化,并且支持日期差額運(yùn)算。
安裝
pip install zhdate
主要功能
1、獲取公歷對應(yīng)的農(nóng)歷日期
2、獲取中文描述農(nóng)歷日期
3、計算公歷距離農(nóng)歷差額
獲取公歷對應(yīng)的農(nóng)歷日期:格式ZhDate.from_datetime(datetime(year, month, day))
print(ZhDate.from_datetime(datetime(2022, 3, 27))) # 農(nóng)歷2022年2月25日
獲取中文描述農(nóng)歷日期:需對結(jié)果調(diào)用chinese()方法
格式ZhDate.from_datetime(datetime(year, month, day)).chinese()
print(ZhDate.from_datetime(datetime(2022, 3, 27)).chinese()) # 二零二二年二月二十五 壬寅年 (虎年)
計算公歷距離農(nóng)歷差額:
格式:difference = lc_day.toordinal() - gc_day.toordinal()
源碼
# -*- coding:utf-8 -*- from zhdate import ZhDate from datetime import datetime def get_chinese_traditional_calendar(date=None): """ :param date: none = now day. :return: """ if date: year, month, day = int(date[:4]), int(date[4:6]), int(date[6:]) else: now = str(datetime.now().strftime('%Y-%m-%d')).split("-") year, month, day = int(now[0]), int(now[1]), int(now[2]) return ZhDate.from_datetime(datetime(year, month, day)) def get_difference_days(date1, date2=None): """ :param date1: :param date2: none = now day :return: """ if date2: year1, month1, day1 = int(date1[:4]), int(date1[4:6]), int(date1[6:]) year2, month2, day2 = int(date2[:4]), int(date2[4:6]), int(date2[6:]) else: now = str(datetime.now().strftime('%Y-%m-%d')).split("-") year1, month1, day1 = int(date1[:4]), int(date1[4:6]), int(date1[6:]) year2, month2, day2 = int(now[0]), int(now[1]), int(now[2]) date2 = f"{year2}{month2}{day2}" one_day = datetime(year2, month2, day2) other_day = datetime(year1, month1, day1) difference = abs(one_day.toordinal() - other_day.toordinal()) print(f'{date1} 距離 {date2} 相差 {difference} 天') return difference def get_difference_chinese_calendar(gc_date, lc_date): """ :param gc_date: the gregorian calendar 公歷 :param lc_day: the lunar calendar 農(nóng)歷 :return: """ year1, month1, day1 = int(gc_date[:4]), int(gc_date[4:6]), int(gc_date[6:]) year2, month2, day2 = int(lc_date[:4]), int(lc_date[4:6]), int(lc_date[6:]) gc_day = datetime(year1, month1, day1) lc_day = ZhDate(year2, month2, day2).to_datetime() difference = lc_day.toordinal() - gc_day.toordinal() print(f'公歷 {gc_date} 距離 農(nóng)歷 {lc_date} 相差 {abs(difference)} 天') return difference if __name__ == '__main__': # 當(dāng)前日期對應(yīng)的農(nóng)歷日期 date1 = get_chinese_traditional_calendar() print(date1) print(date1.chinese()) # 指定日期對應(yīng)的農(nóng)歷日期 date2 = get_chinese_traditional_calendar("20220328") print(date2) print(date2.chinese()) # 公歷日期相差 get_difference_days("20220511") get_difference_days("20220327", "20221001") # 公歷距離農(nóng)歷相差 get_difference_chinese_calendar("20220327", "20220303") # 距離農(nóng)歷三月三 get_difference_chinese_calendar("20220327", "20220505") # 距離端午節(jié) get_difference_chinese_calendar("20220327", "20220815") # 距離中秋節(jié) get_difference_chinese_calendar("20220327", "20220909") # 距離重陽節(jié) get_difference_chinese_calendar("20220327", "20230101") # 距離春節(jié)
以上就是Python利用zhdate模塊實(shí)現(xiàn)農(nóng)歷日期處理的詳細(xì)內(nèi)容,更多關(guān)于Python農(nóng)歷日期處理的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決項(xiàng)目pycharm能運(yùn)行,在終端卻無法運(yùn)行的問題
今天小編就為大家分享一篇解決項(xiàng)目pycharm能運(yùn)行,在終端卻無法運(yùn)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python異常處理機(jī)制結(jié)構(gòu)實(shí)例解析
這篇文章主要介紹了Python異常處理機(jī)制結(jié)構(gòu)實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07利用Python實(shí)現(xiàn)給圖像添加標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)給指定的圖片添加標(biāo)簽,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下2023-07-07在 Django/Flask 開發(fā)服務(wù)器上使用 HTTPS
使用 Django 或 Flask 這種框架開發(fā) web app 的時候一般都會用內(nèi)建服務(wù)器開發(fā)和調(diào)試程序,等程序完成后再移交到生產(chǎn)環(huán)境部署。問題是這些內(nèi)建服務(wù)器通常都不支持 HTTPS,那么我們來探討下開啟https吧2014-07-07關(guān)于Python不換行輸出和不換行輸出end=““不顯示的問題(親測已解決)
這篇文章主要介紹了關(guān)于Python不換行輸出和不換行輸出end=““不顯示的問題(親測已解決),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例
大家好,本篇文章主要講的是用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained)
今天小編就為大家分享一篇PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01