代码如下:
void Main() { //农历转阳历 var lunar = new System.Globalization.ChineseLunisolarCalendar(); var date = lunar.ToDateTime(2020, 1, 1, 0, 0, 0, 0); //将2020年正月初一转为阳历,如果闰月,则月份加1。2020年有闰四月,则闰四月传5, 本年度之后的月份依次类推 Console.WriteLine(date); //阳历转阴历 date = new DateTime(2020, 1, 1); var leapMonth = lunar.GetLeapMonth(date.Year); //获取当年农历闰月 if (leapMonth == 0) //当年没有闰月 { var lunarDate = lunar.GetYear(date) + "年" + lunar.GetMonth(date) + "月" + lunar.GetDayOfMonth(date) + "日"; Console.WriteLine(lunarDate); } else //当年有闰月 { //因闰月之后月份都加了一,所以要减一 var month = lunar.GetMonth(date) >= leapMonth ? lunar.GetMonth(date) - 1 : lunar.GetMonth(date); var lunarDate = lunar.GetYear(date) + "年" + (lunar.GetMonth(date) == leapMonth ? "闰" : string.Empty) + month + "月" + lunar.GetDayOfMonth(date) + "日"; Console.WriteLine(lunarDate); } }