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


Posted in Python onMarch 31, 2022

简介

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农历日期处理的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
使用Python的Tornado框架实现一个简单的WebQQ机器人
Apr 24 Python
Windows下python2.7.8安装图文教程
May 26 Python
Python内存读写操作示例
Jul 18 Python
Python 获取中文字拼音首个字母的方法
Nov 28 Python
浅析Python语言自带的数据结构有哪些
Aug 27 Python
python实现统计代码行数的小工具
Sep 19 Python
Python模拟FTP文件服务器的操作方法
Feb 18 Python
Python拼接字符串的7种方式详解
Mar 19 Python
Python中sys模块功能与用法实例详解
Feb 26 Python
手把手教你如何用Pycharm2020.1.1配置远程连接的详细步骤
Aug 07 Python
利用python 下载bilibili视频
Nov 13 Python
关于python中readlines函数的参数hint的相关知识总结
Jun 24 Python
详解Python中__new__方法的作用
Mar 31 #Python
利用Python将list列表写入文件并读取的方法汇总
Mar 25 #Python
利用Python多线程实现图片下载器
Python实现灰色关联分析与结果可视化的详细代码
聊聊基于pytorch实现Resnet对本地数据集的训练问题
pycharm安装深度学习pytorch的d2l包失败问题解决
利用For循环遍历Python字典的三种方法实例
Mar 25 #Python
You might like
php使用json_encode对变量json编码
2014/04/07 PHP
php实现大文件断点续传下载实例代码
2019/10/01 PHP
Nigma vs Liquid BO3 第一场2.14
2021/03/10 DOTA
Array.prototype.slice.apply的使用方法
2010/03/17 Javascript
js判断一个元素是否为另一个元素的子元素的代码
2012/03/21 Javascript
JS实现向表格行添加新单元格的方法
2015/03/30 Javascript
高性能JavaScript DOM编程(1)
2015/08/11 Javascript
AngularJS指令用法详解
2016/11/02 Javascript
JS正则表达式判断有效数实例代码
2017/03/13 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
vue使用Google地图的实现示例代码
2018/12/19 Javascript
从零开始在NPM上发布一个Vue组件的方法步骤
2018/12/20 Javascript
微信小程序基于picker实现级联菜单
2019/02/15 Javascript
ES6基础之字符串和函数的拓展详解
2019/08/22 Javascript
微信小程序开发摇一摇功能
2019/11/22 Javascript
详解JSON.stringify()的5个秘密特性
2020/05/26 Javascript
如何在Vue中使localStorage具有响应式(思想实验)
2020/07/14 Javascript
vue自动添加浏览器兼容前后缀操作
2020/08/13 Javascript
Python爬取个人微信朋友信息操作示例
2018/08/03 Python
Django中提供的6种缓存方式详解
2019/08/05 Python
python 申请内存空间,用于创建多维数组的实例
2019/12/02 Python
Python timer定时器两种常用方法解析
2020/01/20 Python
TensorFlow MNIST手写数据集的实现方法
2020/02/05 Python
Python 动态变量名定义与调用方法
2020/02/09 Python
python 实现读取csv数据,分类求和 再写进 csv
2020/05/18 Python
使用keras内置的模型进行图片预测实例
2020/06/17 Python
应届生求职推荐信
2013/10/28 职场文书
行政助理岗位职责范文
2013/12/03 职场文书
公务员总结性个人自我评价
2013/12/05 职场文书
小学生班会演讲稿
2014/01/09 职场文书
税务会计岗位职责
2014/02/18 职场文书
物理学专业求职信
2014/07/04 职场文书
趣味运动会新闻稿
2015/07/17 职场文书
2015年信息化建设工作总结
2015/07/23 职场文书
2016公司新年问候语
2015/11/11 职场文书
Mysql数据库group by原理详解
2022/07/07 MySQL