Python阴阳历日期转换

    阳历转阴(殷)历,阴历转阳历,了解一下阴阳历的转换逻辑、闰月的转换。
    农历,古时称为夏历,是中国现行的传统历法, 属于阴历和阳历的合历,根据月相的变化周期一个月,参考太阳回归年为一年的长度,加入二十四节气与设置闫月平均历年与回归年相适应。-- 百度百科

1. 阴阳历偏差计算表
    参考以下所使用python库中定义的1900~2100年的月天数、闰月计算偏差表。2100年以后呢?

2. zhdate 库
    使用数字标识自1900年至2100年的月天数、是否闰月、闰月天数列表,计算偏差得出阴历日。

from zhdate import ZhDate
from datetime import datetime

print(ZhDate.from_datetime(datetime(2023, 9, 17)))
print(ZhDate(2023, 9, 17).to_datetime())

>>>
======================================================
Lunar is:  农历2023年8月3日
Solar is:  2023-10-31 00:00:00

3. lunardate 库

  • 当前日期阳历和阴历显示
import lunardate as ld
import time
from datetime import datetime

print('Today Solar is: ', time.strftime('%Y-%m-%d')) 
print('Today Lunar is: ', ld.LunarDate.today())


y = ld.LunarDate.today().year
m = ld.LunarDate.today().month
d = ld.LunarDate.today().day
sd = str(y) + '-' + str(m) + '-' + str(d)

print('Today Lunar is: ', datetime.strptime(sd, '%Y-%m-%d').date())

>>>
===========================================
Today Solar is:  2023-09-17
Today Lunar is:  LunarDate(2023, 8, 3, 0)
Today Lunar is:  2023-08-03
  • 指定日期(2023-01-01)转阳历和阴历
import lunardate as ld
import time

print('Today is: ', time.strftime('%Y-%m-%d'))
td = [time.gmtime().tm_year, time.gmtime().tm_mon, time.gmtime().tm_mday]
print('Today Lunar is: ', ld.LunarDate.fromSolarDate(td[0],td[1],td[2]))
print('Today Solar is: ', ld.LunarDate(td[0],td[1],td[2]).toSolarDate())

>>>
=============================================
Today is:  2023-09-17
Today Lunar is:  LunarDate(2023, 8, 3, 0)
Today Solar is:  2023-10-31

4. lunar 库 -- CSDN创作助手:python 阳历转阴历

说明:中文版的lunar库,应该是python版本不对,大量需要修正

  • 安装
pip install lunar-python
  • 编码及运行程序 
from lunar import Lunar

# 创建阳历日期对象
date = Lunar.from_date(2022, 1, 1)

# 转换为阴历日期对象
lunar_date = date.to_lunar_date()

# 获取阴历日期信息
print(lunar_date.year, lunar_date.month, lunar_date.day, lunar_date.is_leap)

>>>
=================================================
    date = Lunar.from_date(2022, 1, 1)
AttributeError: type object 'Lunar' has no attribute 'from_date'
  • 错误修改
from lunar import Lunar
from datetime import datetime

# 创建阳历日期对象
date = Lunar.from_date(2022, 1, 1)
date = datetime.now()

# 转换为阴历日期对象
lunar_date = date.to_lunar_date()
lunardate = Lunar.fromDate(date)
print(lunardate)

# 获取阴历日期信息
#print(lunar_date.year, lunar_date.month, lunar_date.day, lunar_date.is_leap)

>>>
===========================================
二〇二三年八月初三

5. lunardate 库 -- CSDN创作助手:python 3 阳历转阴历
    yearInfos作为偏差(offset)生成自1900年到2100年的月天数、是否闰年、闰年月份及天数,计算得出阳历日期对应的阴历日期。

  • 安装
pip install lunardate
  • 编码及运行程序 
import lunardate

# 定义阳历日期
year = 2023
month = 9
day = 17

# 将阳历日期转换为阴历日期
lunar_date = lunardate.LunarDate.fromSolarDate(year, month, day)

# 输出阴历日期
print("阴历日期为:", lunar_date.day, lunar_date.month, lunar_date.year)

# 输出阳历日期
solar_date = lunardate.LunarDate(year,month,day).toSolarDate()
print("阳历日期为:", solar_date.day, solar_date.month, solar_date.year)

>>>
==========================================
阴历日期为: 3 8 2023
阳历日期为: 31 10 2023

6. 闰

  • 闰月,Leap Month,针对阴历、农历。
    - 阴阳历逢闰年所加的1个月,十九年七闰的方法:在农历十九年中,有十二个平年,一平年十二个月;有七个闰年,一闰年十三个月。
    - 农历每二至三年增加的一个月,即每2~3年置1闰
    - 闰月,大月为30日,小月为29日
    - 闰月表参考 闰月(阴阳合历中的历法概念)_百度百科
  • 闰年,Leap Year,针对阳历
    - 闰年366天,平年365天
    - 闰日2月为29天,平日2月为28天
    - 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)
    - 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)
    - 参考 闰年_百度百科
yinput = input("输入年份:")
YEAR = int(yinput)
if (YEAR % 4 == 0 and YEAR % 100 != 0) or (YEAR % 400 == 0 and YEAR % 3200 != 0) or YEAR % 172800 == 0:    
# 似乎一直在用的是 (YEAR % 4 == 0 and YEAR % 100 != 0) or (YEAR % 400 == 0)
    print (YEAR, ": 闰年")
else:
    print (YEAR, ": 平年")

========================================
>>>
输入年份:2020
2020 : 闰年

>>>
输入年份:1000
1000 : 平年

>>>
输入年份:2000
2000 : 闰年

>>>
输入年份:2023
2023 : 平年

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值