最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python利用zhdate模塊實(shí)現(xiàn)農(nóng)歷日期處理

 更新時(shí)間:2022年03月31日 10:53:40   作者:三爺帶你飛  
zhdate模塊統(tǒng)計(jì)從1900年到2100年的農(nóng)歷月份數(shù)據(jù)代碼,支持農(nóng)歷和公歷之間的轉(zhuǎn)化,并且支持日期差額運(yùn)算。本文將利用這一模塊實(shí)現(xiàn)農(nóng)歷日期的處理,需要的可以參考一下

簡介

zhdate模塊統(tǒng)計(jì)從1900年到2100年的農(nóng)歷月份數(shù)據(jù)代碼,支持農(nóng)歷和公歷之間的轉(zhuǎn)化,并且支持日期差額運(yùn)算。

安裝

pip install zhdate

主要功能

1、獲取公歷對(duì)應(yīng)的農(nóng)歷日期

2、獲取中文描述農(nóng)歷日期

3、計(jì)算公歷距離農(nóng)歷差額

獲取公歷對(duì)應(yīng)的農(nóng)歷日期:格式ZhDate.from_datetime(datetime(year, month, day))

print(ZhDate.from_datetime(datetime(2022, 3, 27)))
# 農(nóng)歷2022年2月25日

獲取中文描述農(nóng)歷日期:需對(duì)結(jié)果調(diào)用chinese()方法

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

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

計(jì)算公歷距離農(nóng)歷差額:

格式: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 農(nóng)歷
    :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} 距離 農(nóng)歷 {lc_date} 相差 {abs(difference)} 天')
    return difference


if __name__ == '__main__':
    # 當(dāng)前日期對(duì)應(yīng)的農(nóng)歷日期
    date1 = get_chinese_traditional_calendar()
    print(date1)
    print(date1.chinese())

    # 指定日期對(duì)應(yīng)的農(nóng)歷日期
    date2 = get_chinese_traditional_calendar("20220328")
    print(date2)
    print(date2.chinese())

    # 公歷日期相差
    get_difference_days("20220511")
    get_difference_days("20220327", "20221001")

    # 公歷距離農(nóng)歷相差
    get_difference_chinese_calendar("20220327", "20220303")  # 距離農(nóng)歷三月三
    get_difference_chinese_calendar("20220327", "20220505")  # 距離端午節(jié)
    get_difference_chinese_calendar("20220327", "20220815")  # 距離中秋節(jié)
    get_difference_chinese_calendar("20220327", "20220909")  # 距離重陽節(jié)
    get_difference_chinese_calendar("20220327", "20230101")  # 距離春節(jié)

以上就是Python利用zhdate模塊實(shí)現(xiàn)農(nóng)歷日期處理的詳細(xì)內(nèi)容,更多關(guān)于Python農(nóng)歷日期處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • opencv+python實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊圖像,輸出該點(diǎn)的RGB和HSV值

    opencv+python實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊圖像,輸出該點(diǎn)的RGB和HSV值

    這篇文章主要介紹了opencv+python實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊圖像,輸出該點(diǎn)的RGB和HSV值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python中@classmethod和@staticmethod的區(qū)別

    Python中@classmethod和@staticmethod的區(qū)別

    本文主要介紹了Python中@classmethod和@staticmethod的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • pytorch動(dòng)態(tài)網(wǎng)絡(luò)以及權(quán)重共享實(shí)例

    pytorch動(dòng)態(tài)網(wǎng)絡(luò)以及權(quán)重共享實(shí)例

    今天小編就為大家分享一篇pytorch動(dòng)態(tài)網(wǎng)絡(luò)以及權(quán)重共享實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python os庫常用操作代碼匯總

    Python os庫常用操作代碼匯總

    這篇文章主要介紹了Python os庫常用操作代碼匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python獲取list下標(biāo)及其值的簡單方法

    python獲取list下標(biāo)及其值的簡單方法

    下面小編就為大家?guī)硪黄猵ython獲取list下標(biāo)及其值的簡單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-09-09
  • Python基于easygui實(shí)現(xiàn)pdf和word轉(zhuǎn)換小程序

    Python基于easygui實(shí)現(xiàn)pdf和word轉(zhuǎn)換小程序

    這篇文章主要為大家詳細(xì)介紹了Python如何基于easygui實(shí)現(xiàn)pdf和word轉(zhuǎn)換小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • pycharm無法導(dǎo)入lxml的解決辦法

    pycharm無法導(dǎo)入lxml的解決辦法

    這篇文章主要介紹了pycharm無法導(dǎo)入lxml的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python將字母轉(zhuǎn)化為數(shù)字實(shí)例方法

    python將字母轉(zhuǎn)化為數(shù)字實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于python如何將字母轉(zhuǎn)化為數(shù)字的相關(guān)實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-10-10
  • Pandas?DataFrame列快速轉(zhuǎn)換為列表(3秒學(xué)會(huì)!)

    Pandas?DataFrame列快速轉(zhuǎn)換為列表(3秒學(xué)會(huì)!)

    這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame列如何快速轉(zhuǎn)換為列表的相關(guān)資料,在Python的pandas庫中可以使用DataFrame的tolist()方法將DataFrame轉(zhuǎn)化為列表,需要的朋友可以參考下
    2023-10-10
  • python字符串替換示例

    python字符串替換示例

    這篇文章主要介紹了python字符串替換示例,需要的朋友可以參考下
    2014-04-04

最新評(píng)論

莫力| 蒙山县| 无为县| 山阴县| 宜宾市| 米林县| 眉山市| 南投市| 萝北县| 华安县| 武川县| 行唐县| 崇仁县| 米易县| 特克斯县| 鲁山县| 格尔木市| 赫章县| 迭部县| 贵德县| 宣威市| 刚察县| 苗栗县| 习水县| 讷河市| 蛟河市| 仲巴县| 安阳市| 那坡县| 安多县| 东源县| 延川县| 肇源县| 新巴尔虎右旗| 紫云| 红河县| 栾城县| 大城县| 苗栗市| 林州市| 通榆县|