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 相关文章推荐
400多行Python代码实现了一个FTP服务器
May 10 Python
打开电脑上的QQ的python代码
Feb 10 Python
详细解析Python中的变量的数据类型
May 13 Python
Python使用cookielib模块操作cookie的实例教程
Jul 12 Python
django开发之settings.py中变量的全局引用详解
Mar 29 Python
用Python将mysql数据导出成json的方法
Aug 21 Python
python调用webservice接口的实现
Jul 12 Python
flask框架路由常用定义方式总结
Jul 23 Python
在django-xadmin中APScheduler的启动初始化实例
Nov 15 Python
Tensorflow 实现将图像与标签数据转化为tfRecord文件
Feb 17 Python
python操作xlsx格式文件并读取
Jun 02 Python
Python3的进程和线程你了解吗
Mar 16 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 之Section与Cookie使用总结
2012/09/14 PHP
基于JQuery+PHP编写砸金蛋中奖程序
2015/09/08 PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
2018/09/26 PHP
一个页面元素appendchild追加到另一个页面元素的问题
2013/01/27 Javascript
javascript按位非运算符的使用方法
2013/11/14 Javascript
js获取IP地址的方法小结
2014/07/01 Javascript
jQuery实现单击和鼠标感应事件
2015/02/01 Javascript
JQuery显示隐藏DIV的方法及代码实例
2015/04/16 Javascript
jQuery选择器及jquery案例详解(必看)
2016/05/20 Javascript
AngularJS基础 ng-open 指令简单实例
2016/08/02 Javascript
教你一步步用jQyery实现轮播器
2016/12/18 Javascript
浅析 NodeJs 的几种文件路径
2017/06/07 NodeJs
全选复选框JavaScript编写小结(附代码)
2017/08/16 Javascript
浅谈JS函数节流防抖
2017/10/18 Javascript
Node批量爬取头条视频并保存方法
2018/09/20 Javascript
浅谈Node框架接入ELK实践总结
2019/02/22 Javascript
在微信小程序中使用vant的方法
2019/06/07 Javascript
解决mui框架中switch开关通过js控制开或者关状态时小圆点不动的问题
2019/09/03 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
OpenLayers3加载常用控件使用方法详解
2020/09/25 Javascript
vue祖孙组件之间的数据传递案例
2020/12/07 Vue.js
Python通过90行代码搭建一个音乐搜索工具
2015/07/29 Python
python 安装virtualenv和virtualenvwrapper的方法
2017/01/13 Python
浅谈Python批处理文件夹中的txt文件
2019/03/11 Python
python SQLAlchemy 中的Engine详解
2019/07/04 Python
django框架用户权限中的session缓存到redis中的方法
2019/08/06 Python
Python GUI编程学习笔记之tkinter控件的介绍及基本使用方法详解
2020/03/30 Python
Python与C/C++的相互调用案例
2021/03/04 Python
社团2014年植树节活动总结
2014/03/11 职场文书
我为自己代言广告词
2014/03/18 职场文书
自荐信格式范文
2015/03/04 职场文书
复兴之路观后感3000字
2015/06/02 职场文书
《当代神农氏》教学反思
2016/02/23 职场文书
2016年第二十届“母亲节暨幸福工程救助贫困母亲活动日”活动总结
2016/04/06 职场文书
2019年行政人事个人工作总结范本!
2019/07/19 职场文书
Pandas数据类型之category的用法
2021/06/28 Python