vs获取农历日期格式为:牛年 辛丑 四月十六

之前的1921年的出错了,又在网上找了一个改了下,没找到原链接,最后出现的格式是:牛年  辛丑  四月十六,有找到的可以放在评论

    class LunDay//当天日期变为阴历
    {
        System.Globalization.ChineseLunisolarCalendar ChineseCalendar = new System.Globalization.ChineseLunisolarCalendar();
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };// 十天干
        private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };// 十二地支
        private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };// 十二生肖
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };// 农历月
        private static string[] days1 = { "初", "十", "廿", "三" };// 农历日
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };

        private static string GetLunisolarYear(int year)// 返回农历年
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;

                return string.Concat(sx[dzIndex],"年","  ",tg[tgIndex], dz[dzIndex] );
            }

            throw new ArgumentOutOfRangeException("无效的年份!");
        }
        private static string GetLunisolarMonth(int month)// 返回农历月
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }

            throw new ArgumentOutOfRangeException("无效的月份!");
        }
        private static string GetLunisolarDay(int day)// 返回农历日
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }
            throw new ArgumentOutOfRangeException("无效的日!");
        }

        public  string GetLunisolarCalendar(DateTime dateTime)// 返回农历日期
        {
            bool isleap = false;
            LunDay lunDay = new LunDay();
            int year = lunDay.ChineseCalendar.GetYear(dateTime);
            int month = lunDay.ChineseCalendar.GetMonth(dateTime);
            int day = lunDay.ChineseCalendar.GetDayOfMonth(dateTime);
            //获取闰月,0 则表示没有闰月.当之前的月份都闰月存在,则需要当前月份减1
            int leapMonth = lunDay.ChineseCalendar.GetLeapMonth(year);
            if (leapMonth > 0)
            {
                if (leapMonth <= month)
                {
                    if (leapMonth == month)
                    {
                        isleap = true;  //农历闰月
                    }   
                    month--;  //月份修正
                }
            }
            return string.Concat(GetLunisolarYear(year),"  ", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }
    }
}

调用时

  LunDay lunday = new LunDay();//计算阴历日期
  label8.Text = lunday.GetLunisolarCalendar(DateTime.Now);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值