zoukankan      html  css  js  c++  java
  • c# 公元转农历

     代码如下:

    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);
        }
    }
  • 相关阅读:
    Haproxy 【转载】
    Nginx介绍
    Day 13 进程和线程
    运维第一课
    面试bb
    Day 12 字符串和正则表达式
    Day 11 文件和异常
    Day10 图形用户界面和游戏开发
    Day9 面向对象进阶
    day8 面向对象编程基础
  • 原文地址:https://www.cnblogs.com/nanfei/p/10737208.html
Copyright © 2011-2022 走看看