網站首頁 編程語言 正文
簡介
zhdate模塊統計從1900年到2100年的農歷月份數據代碼,支持農歷和公歷之間的轉化,并且支持日期差額運算。
安裝
pip install zhdate
主要功能
1、獲取公歷對應的農歷日期
2、獲取中文描述農歷日期
3、計算公歷距離農歷差額
獲取公歷對應的農歷日期:格式ZhDate.from_datetime(datetime(year, month, day))
print(ZhDate.from_datetime(datetime(2022, 3, 27))) # 農歷2022年2月25日
獲取中文描述農歷日期:需對結果調用chinese()方法
格式ZhDate.from_datetime(datetime(year, month, day)).chinese()
print(ZhDate.from_datetime(datetime(2022, 3, 27)).chinese()) # 二零二二年二月二十五 壬寅年 (虎年)
計算公歷距離農歷差額:
格式: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 農歷 :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} 距離 農歷 {lc_date} 相差 {abs(difference)} 天') return difference if __name__ == '__main__': # 當前日期對應的農歷日期 date1 = get_chinese_traditional_calendar() print(date1) print(date1.chinese()) # 指定日期對應的農歷日期 date2 = get_chinese_traditional_calendar("20220328") print(date2) print(date2.chinese()) # 公歷日期相差 get_difference_days("20220511") get_difference_days("20220327", "20221001") # 公歷距離農歷相差 get_difference_chinese_calendar("20220327", "20220303") # 距離農歷三月三 get_difference_chinese_calendar("20220327", "20220505") # 距離端午節 get_difference_chinese_calendar("20220327", "20220815") # 距離中秋節 get_difference_chinese_calendar("20220327", "20220909") # 距離重陽節 get_difference_chinese_calendar("20220327", "20230101") # 距離春節
原文鏈接:https://blog.csdn.net/hzblucky1314/article/details/123861693
相關推薦
- 2022-10-14 nginx和apache的區別
- 2022-11-24 詳解python中@classmethod和@staticmethod方法_python
- 2022-09-10 詳解Python腳本如何設置試用期_python
- 2022-04-16 python中的?sorted()函數和sort()方法區別_python
- 2022-05-28 使用pandas計算環比和同比的方法實例_python
- 2022-04-19 C#中的類繼承詳解_C#教程
- 2022-09-12 輕量級域名解析服務器之dnsmasq的介紹與部署_服務器其它
- 2022-09-20 Redis安全策略詳解_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支