from datetime import datetime
import cnlunar
# 数字转中文函数
def num_to_chinese(num):
chinese_numerals = {
0: "零", 1: "一", 2: "二", 3: "三", 4: "四",
5: "五", 6: "六", 7: "七", 8: "八", 9: "九",
10: "十", 11: "十一", 12: "十二", 13: "十三",
14: "十四", 15: "十五", 16: "十六", 17: "十七",
18: "十八", 19: "十九", 20: "二十"
}
if num in chinese_numerals:
return chinese_numerals[num]
elif num < 30:
return "二十" + chinese_numerals[num - 20]
elif num < 40:
return "三十" + chinese_numerals[num - 30]
else:
return str(num)
# 获取当天日期
today = datetime.today()
today_date = today.strftime("%Y-%m-%d") # 格式化为年月日
# 使用 cnlunar 获取农历日期
lunar = cnlunar.Lunar(today) # 传入今天的日期
# 获取农历年份、月份和日期
lunar_year_chinese = lunar.get_lunarYearCN() # 获取农历年份中文
lunar_month_chinese = lunar.get_lunarMonthCN() # 获取农历月份中文
lunar_day_chinese = num_to_chinese(lunar.lunarDay) # 获取农历日期中文并转为中文数字
# 输出结果
print("日期:", today_date)
print("农历:", lunar_year_chinese, lunar_month_chinese, lunar_day_chinese)