iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python利用zhdate模块实现农历日期处理
  • 856
分享到

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

2024-04-02 19:04:59 856人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录简介安装主要功能源码简介 zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。 安装 pip install zhda

简介

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农历日期处理的资料请关注编程网其它相关文章!

--结束END--

本文标题: Python利用zhdate模块实现农历日期处理

本文链接: https://www.lsjlt.com/news/144573.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python利用zhdate模块实现农历日期处理
    目录简介安装主要功能源码简介 zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。 安装 pip install zhda...
    99+
    2022-11-13
  • Python常用标准库及第三方库3-日期、时间处理模块
    时间处理是编程中一个比较常见的情况,比如转换时间类型:后端接口传参时通常是传递时间戳,前台拿到接口返回值中的时间戳通常需要格式化后再进行展示。在Python中,处理时间的模块有time、datetime。一、time模块1.time模块简介...
    99+
    2023-05-14
    Python 模块 内建库
  • 如何利用Python日志打包load实现更高效的数据处理?
    在数据处理过程中,我们通常需要大量的数据读取和处理。而Python作为一门强大的编程语言,提供了丰富的工具来简化这一过程。其中,Python日志打包load技术是一种非常有效的数据处理技术。 Python日志打包load技术可以用来将多个...
    99+
    2023-10-29
    日志 打包 load
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作