Python利用zhdate模块实现农历日期处理

作者:三爷带你飞 时间:2023-03-07 22:10:20 

简介

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

标签:Python,农历,日期
0
投稿

猜你喜欢

  • python实现K折交叉验证

    2023-06-08 18:49:01
  • IPython 8.0 Python 命令行交互工具

    2022-10-24 09:17:54
  • .NET Core2.1如何获取自定义配置文件信息详解

    2023-07-17 16:26:34
  • Vue.js directive自定义指令详解

    2024-05-28 15:46:18
  • Python 两个列表的差集、并集和交集实现代码

    2021-12-26 18:11:01
  • python __init__与 __new__的区别

    2022-05-04 01:27:57
  • uniqueidentifier转换成varchar数据类型的sql语句

    2011-09-30 11:17:48
  • 通过python+selenium3实现浏览器刷简书文章阅读量

    2022-11-09 09:04:37
  • Python面向对象之类和对象属性的增删改查操作示例

    2021-11-06 14:20:40
  • 不同分辨率下的自适用宽度

    2008-11-24 12:58:00
  • Pyscript使用本地Pyodide配置步骤

    2021-06-12 06:58:49
  • 用 Django 开发一个 Python Web API的方法步骤

    2021-05-24 02:36:11
  • vue axios form-data格式传输数据和文件方式

    2024-04-30 10:18:57
  • Python的pdfplumber库将pdf转为图片的实现

    2021-08-31 20:43:27
  • golang json性能分析详解

    2024-05-09 14:57:46
  • ASP开发中可能遇到的错误信息中文说明大全(整理收集)第1/2页

    2010-07-02 09:50:31
  • 解决os.path.isdir() 判断文件夹却返回false的问题

    2022-11-07 18:11:37
  • numpy:np.newaxis 实现将行向量转换成列向量

    2023-07-13 03:14:39
  • 解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题

    2022-12-06 16:17:37
  • php比较多维数组中值的大小排序实现代码

    2023-11-24 13:22:32
  • asp之家 网络编程 m.aspxhome.com