龙空技术网

Python公历和农历转换(阳历和阴历转换)

Pgabc 220

前言:

当前咱们对“公历转农历算法”可能比较着重,同学们都需要学习一些“公历转农历算法”的相关文章。那么小编也在网上网罗了一些有关“公历转农历算法””的相关资讯,希望朋友们能喜欢,你们快快来学习一下吧!

"""公历和农历转换(阳历和阴历转换)"""from datetime import dateimport sxtwldef moon_to_solar(yyyy_wm, mm_wm, dd_wm):    # 阴历转阳历    lunar = sxtwl.Lunar()    solar_day = lunar.getDayByLunar(yyyy_wm, mm_wm, dd_wm)    solar_day_wm = "公历(阳历):{0}年{1}月{2}日".format(solar_day.y, solar_day.m, solar_day.d)    print("公历(阳历):{0}年{1}月{2}日".format(solar_day.y, solar_day.m, solar_day.d))    return solar_day_wmdef solar_to_moon(yyyy_wm, mm_wm, dd_wm):    # 阳历转阴历     ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十"]    rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十", \           u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九", \           u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]    yyyy_china = {0: '0', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七', 8: '八', 9: '九'}    yyyy_ = str(yyyy_wm)    yyyy_nv = ''    for i in range(len(yyyy_)):        wm_ = yyyy_[i]        nv_ = yyyy_china[int(wm_)]        yyyy_nv += nv_    lunar = sxtwl.Lunar()    lunar_day = lunar.getDayBySolar(yyyy_wm, mm_wm, dd_wm)    if (lunar_day.Lleap):        lunar_day_wm = "农历(阴历):{0}年{1}月{2}".format(yyyy_nv, ymc[lunar_day.Lmc], rmc[lunar_day.Ldi])        print("阴历(农历):{0}年{1}月{2}".format(yyyy_nv, ymc[lunar_day.Lmc], rmc[lunar_day.Ldi]))    else:        lunar_day_wm = "农历(阴历):{0}年{1}月{2}".format(yyyy_nv, ymc[lunar_day.Lmc], rmc[lunar_day.Ldi])        print("农历(阴历):{0}年{1}月{2}".format(yyyy_nv, ymc[lunar_day.Lmc], rmc[lunar_day.Ldi]))    return lunar_day_wmif __name__ == '__main__':    moon_to_solar(1998, 5, 22)    solar_to_moon(1997, 12, 26)    """运行结果"""    公历(阳历):1998年6月16日    农历(阴历):一九九七年十一月廿七    Process finished with exit code 0    """    Pgabc 2022000012    author : Pgabc        """

标签: #公历转农历算法