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

 更新时间:2022年03月31日 10:53:40   作者:三爷带你飞  
zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。本文将利用这一模块实现农历日期的处理,需要的可以参考一下

简介

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")  # 距离春节

以上就是Python利用zhdate模块实现农历日期处理的详细内容,更多关于Python农历日期处理的资料请关注脚本之家其它相关文章!

相关文章

  • opencv+python实现鼠标点击图像,输出该点的RGB和HSV值

    opencv+python实现鼠标点击图像,输出该点的RGB和HSV值

    这篇文章主要介绍了opencv+python实现鼠标点击图像,输出该点的RGB和HSV值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python中@classmethod和@staticmethod的区别

    Python中@classmethod和@staticmethod的区别

    本文主要介绍了Python中@classmethod和@staticmethod的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-01-01
  • pytorch动态网络以及权重共享实例

    pytorch动态网络以及权重共享实例

    今天小编就为大家分享一篇pytorch动态网络以及权重共享实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python os库常用操作代码汇总

    Python os库常用操作代码汇总

    这篇文章主要介绍了Python os库常用操作代码汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • python获取list下标及其值的简单方法

    python获取list下标及其值的简单方法

    下面小编就为大家带来一篇python获取list下标及其值的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Python基于easygui实现pdf和word转换小程序

    Python基于easygui实现pdf和word转换小程序

    这篇文章主要为大家详细介绍了Python如何基于easygui实现pdf和word转换小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-04-04
  • pycharm无法导入lxml的解决办法

    pycharm无法导入lxml的解决办法

    这篇文章主要介绍了pycharm无法导入lxml的解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • python将字母转化为数字实例方法

    python将字母转化为数字实例方法

    在本篇文章里小编给大家整理的是关于python如何将字母转化为数字的相关实例内容,有需要的朋友们可以学习下。
    2019-10-10
  • Pandas DataFrame列快速转换为列表(3秒学会!)

    Pandas DataFrame列快速转换为列表(3秒学会!)

    这篇文章主要给大家介绍了关于Pandas DataFrame列如何快速转换为列表的相关资料,在Python的pandas库中可以使用DataFrame的tolist()方法将DataFrame转化为列表,需要的朋友可以参考下
    2023-10-10
  • python字符串替换示例

    python字符串替换示例

    这篇文章主要介绍了python字符串替换示例,需要的朋友可以参考下
    2014-04-04

最新评论