代码如下:

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);
    }
}

 

相关文章:

  • 2021-04-03
  • 2021-06-22
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-01-19
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2022-02-12
  • 2022-12-23
  • 2022-03-03
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案