Python利用zhdate模塊實現(xiàn)農(nóng)歷日期處理
簡介
zhdate模塊統(tǒng)計從1900年到2100年的農(nóng)歷月份數(shù)據(jù)代碼,支持農(nóng)歷和公歷之間的轉(zhuǎn)化,并且支持日期差額運算。
安裝
pip install zhdate
主要功能
1、獲取公歷對應的農(nóng)歷日期
2、獲取中文描述農(nóng)歷日期
3、計算公歷距離農(nó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__': # 當前日期對應的農(nóng)歷日期 date1 = get_chinese_traditional_calendar() print(date1) print(date1.chinese()) # 指定日期對應的農(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模塊實現(xiàn)農(nóng)歷日期處理的詳細內(nèi)容,更多關于Python農(nóng)歷日期處理的資料請關注腳本之家其它相關文章!
相關文章
在 Django/Flask 開發(fā)服務器上使用 HTTPS
使用 Django 或 Flask 這種框架開發(fā) web app 的時候一般都會用內(nèi)建服務器開發(fā)和調(diào)試程序,等程序完成后再移交到生產(chǎn)環(huán)境部署。問題是這些內(nèi)建服務器通常都不支持 HTTPS,那么我們來探討下開啟https吧2014-07-07關于Python不換行輸出和不換行輸出end=““不顯示的問題(親測已解決)
這篇文章主要介紹了關于Python不換行輸出和不換行輸出end=““不顯示的問題(親測已解決),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10