Python:zhdate模块农历日期处理

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

微信公众号:玩转测试开发
欢迎关注,共同进步,谢谢!

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值