python Borax实现农历和公历的日期相互转换

	最近,看到有个网友在豆瓣上求助,想通过提取身份证号码中的日期并将其转换为公历以确定生日,本文先通过python中Borax模块实现农历和公历的互相转换,后面在通过pandas实现网友的要求。
	关于borax的文档可以查阅[https://www.bookstack.cn/read/borax/docs-guides-lunardate.md](https://www.bookstack.cn/read/borax/docs-guides-lunardate.md),本文做简要介绍
	1、安装Borax
	pip install borax或者 python -m pip install borax
	2、功能实现
# 导包
>>> from borax.calendars.lunardate import LunarDate
>>> today = LunarDate.today()
>>> today
LunarDate(2020, 1, 17, 0)
# 农历有很多属性,包括属相,日期(初一,初二等),月份(正月等)特点是均带有cn开头的
>>> print(today.cn_str())
二〇二〇年正月十七
# 获取属相
>>> today.animal
'鼠'
# 获取不同的年月日结果
>>> today.year
2020
>>> today.month
1
>>> today.day
17
>>> today.cn_year
'二〇二〇年'
>>> today.cn_month
'正月'
>>> today.cn_day
'十七'
# 农历转换为公历
>>> today.to_solar_date()
datetime.date(2020, 2, 10)
# 构建一个农历日期
>>> today = LunarDate(2020, 1, 21, 0)
# 公历转农历
>>> today = LunarDate.from_solar_date(2020, 2, 10)
>>> today
LunarDate(2020, 1, 17, 0)
今天就分享到这,简要的记录下borax的这个强大功能。如果有兴趣,欢迎关注微信公众号python小工具。

在这里插入图片描述

发布了5 篇原创文章 · 获赞 1 · 访问量 102

猜你喜欢

转载自blog.csdn.net/weixin_45144170/article/details/104247229