脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python利用zhdate模块实现农历日期处理

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

2022-11-20 12:01三爷带你飞 Python

zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。本文将利用这一模块实现农历日期的处理,需要的可以参考一下

简介

zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。

安装

?
1
pip install zhdate

主要功能

1、获取公历对应的农历日期

2、获取中文描述农历日期

3、计算公历距离农历差额

获取公历对应的农历日期:格式ZhDate.from_datetime(datetime(year, month, day))

?
1
2
print(ZhDate.from_datetime(datetime(2022, 3, 27)))
# 农历2022年2月25日

获取中文描述农历日期:需对结果调用chinese()方法

格式ZhDate.from_datetime(datetime(year, month, day)).chinese()

?
1
2
print(ZhDate.from_datetime(datetime(2022, 3, 27)).chinese())
# 二零二二年二月二十五 壬寅年 (虎年)

计算公历距离农历差额:

格式:difference = lc_day.toordinal() - gc_day.toordinal()

源码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- 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农历日期处理的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/hzblucky1314/article/details/123861693

延伸 · 阅读

精彩推荐
  • PythonPython基于sftp及rsa密匙实现远程拷贝文件的方法

    Python基于sftp及rsa密匙实现远程拷贝文件的方法

    这篇文章主要介绍了Python基于sftp及rsa密匙实现远程拷贝文件的方法,结合实例形式分析了基于RSA秘钥远程登陆及文件操作的相关技巧,需要的朋友可以参考下...

    mo_guang6062020-09-08
  • PythonPython自定义线程类简单示例

    Python自定义线程类简单示例

    这篇文章主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下...

    chengqiuming12182021-01-24
  • PythonPycharm学习教程(3) 代码运行调试

    Pycharm学习教程(3) 代码运行调试

    这篇文章主要为大家详细介绍了最全的Pycharm学习教程第三篇代码运行调试,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    山在岭就在3932020-10-06
  • Python在Python的列表中利用remove()方法删除元素的教程

    在Python的列表中利用remove()方法删除元素的教程

    这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区别,需要的朋友可以参考下 ...

    脚本之家9832020-07-04
  • Python浅析Python编写函数装饰器

    浅析Python编写函数装饰器

    这篇文章主要介绍了Python编写函数装饰器的相关资料,需要的朋友可以参考下 ...

    Gavin3202020-08-17
  • Pythonpython 调用win32pai 操作cmd的方法

    python 调用win32pai 操作cmd的方法

    下面小编就为大家带来一篇python 调用win32pai 操作cmd的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    脚本之家4482020-11-13
  • Python使用python 爬虫抓站的一些技巧总结

    使用python 爬虫抓站的一些技巧总结

    这篇文章主要介绍了用 python 爬虫抓站的一些技巧总结,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    脚本之家9602021-01-03
  • Python5分钟 Pipenv 上手指南

    5分钟 Pipenv 上手指南

    这篇文章主要介绍了5分钟 Pipenv 上手指南,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    FOOFISH-PYTHON之禅6422021-05-05