C# DateTime公历转中国农历

本文介绍了如何在C#中使用.NET的ChineseLunisolarCalendar类将公历转换为中国农历,包括农历年的计算、月份和日期的表示方法,以及示例代码的详细说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

C#: 公历转中国农历

农历是中国现行的传统历法(农历和阴历是有区别的哦),是根据月相的变化周期来计算时间。

与公历不同,农历的年份分为平年闰年,平年为十二个月,闰年为十三个月,月份分为大月和小月,大月三十天,小月二十九天。

在.net中提供了ChineseLunisolarCalendar实现公历转农历算法,下面是一个C#示例代码,完整的展示了如何将公历转换为农历:

    /// <summary>
    /// 获取中国农历
    /// </summary>
    public class DateHelper
    {
        ///<summary>
        /// 实例化
        ///</summary>
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();

        ///<summary>
        /// 十天干
        ///</summary>
        private static string[] TG = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

        ///<summary>
        /// 十二地支
        ///</summary>
        private static string[] DZ = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

        ///<summary>
        /// 十二生肖
        ///</summary>
        private static string[] SX = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

        ///<summary>
        /// 农历月
        ///</summary>
        ///<return></return s>
        private static string[] MONTHS = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };

        ///<summary>
        /// 农历日
        ///</summary>
        private static string[] DYAS1 = { "初", "十", "廿", "三" };

        ///<summary>
        /// 农历日
        ///</summary>
        private static string[] DAYS2 = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

        ///<summary>
        /// 返回农历天干地支年
        ///</summary>
        ///<param name="year">农历年</param>
        ///<return>甲子[鼠]</return s>
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;

                return string.Concat(TG[tgIndex], DZ[dzIndex], "[", SX[dzIndex], "]");
            }
            throw new ArgumentOutOfRangeException("无效的年份!");
        }

        ///<summary>
        /// 返回农历月
        ///</summary>
        ///<param name="month">月份</param>
        ///<return>十二(腊)</return s>
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return MONTHS[month - 1];
            }
            throw new ArgumentOutOfRangeException("无效的月份!");
        }

        ///<summary>
        /// 返回农历日
        ///</summary>
        ///<param name="day">天</param>
        ///<return>初一</return s>
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(DYAS1[(day - 1) / 10], DAYS2[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(DAYS2[(day - 1) / 10], DYAS1[1]);
                }
            }
            throw new ArgumentOutOfRangeException("无效的日!");
        }

        ///<summary>
        /// 根据公历获取农历日期
        ///</summary>
        ///<param name="datetime">公历日期</param>
        ///<return>农历一月初一</return s>
        public static string GetChineseDate(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //获取闰月, 0 则表示没有闰月
            int leapMonth = ChineseCalendar.GetLeapMonth(year);

            //如果有闰月,之后都需要减月
            if (leapMonth > 0)
            {
                if (month >= leapMonth)
                {
                    //闰月
                    month--;
                }
            }
            return string.Concat("农历", GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }

        ///<summary>
        /// 根据公历获取农历日期
        ///</summary>
        ///<param name="datetime">公历日期</param>
        ///<return>甲子[鼠]闰一月初一</return s>
        public static string GetChineseDateTime(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //获取闰月, 0 则表示没有闰月
            int leapMonth = ChineseCalendar.GetLeapMonth(year);

            bool isleap = false;

            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {
                    //闰月
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }
            return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亿码当先

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值