C#按农历计算节日

初始化数据

        private const int MinYear = 1900;

        private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
            new LunarHolidayStruct(1, 1, 1, "春节"),
            new LunarHolidayStruct(1, 15, 0, "元宵节"),
            new LunarHolidayStruct(5, 5, 0, "端午节"),
            new LunarHolidayStruct(7, 7, 0, "七夕情人节"),
            new LunarHolidayStruct(7, 15, 0, "中元节 盂兰盆节"),
            new LunarHolidayStruct(8, 15, 0, "中秋节"),
            new LunarHolidayStruct(9, 9, 0, "重阳节"),
            new LunarHolidayStruct(12, 8, 0, "腊八节"),
            new LunarHolidayStruct(12, 23, 0, "北方小年(扫房)"),
            new LunarHolidayStruct(12, 24, 0, "南方小年(掸尘)"),
        };

        private static int[] LunarDateArray = new int[]{
0x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
0x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
0x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
0x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
0x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
0x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
0x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
0x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
0x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
0x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
0x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
0x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
0x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
0x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
0x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
0x14B63
                };

结构体

  private struct LunarHolidayStruct
  {
      public int Month;
      public int Day;
      public int Recess;
      public string HolidayName;


      public LunarHolidayStruct(int month, int day, int recess, string name)
      {
          Month = month;
          Day = day;
          Recess = recess;
          HolidayName = name;
      }
  }

调用方法

   private static bool BitTest32(int num, int bitpostion)
   {
       if ((bitpostion > 31) || (bitpostion < 0))
           throw new Exception();
       int bit = 1 << bitpostion;
       if ((num & bit) == 0)
       {
           return false;
       }
       else
       {
           return true;
       }
   }

   private static int GetChineseMonthDays(int year, int month)
   {
       if (BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), (16 - month)))
       {
           return 30;
       }
       else
       {
           return 29;
       }
   }
   public static string ChineseCalendarHoliday(bool IsLeapMonth, int year, int month, int day)
   {

       string tempStr = "";
       if (IsLeapMonth == false) //闰月不计算节日
       {
           foreach (LunarHolidayStruct lh in lHolidayInfo)
           {
               if ((lh.Month == month) && (lh.Day == day))
               {


                   tempStr = lh.HolidayName;
                   break;


               }
           }

           //对除夕进行特别处理
           if (month == 12)
           {
               int i = GetChineseMonthDays(year, 12); //计算当年农历12月的总天数
               if (day == i) //如果为最后一天
               {
                   tempStr = "除夕";
               }
           }
       }
       return tempStr;
   }

调用主方法 ChineseCalendarHoliday(false, 2024, 7, 7)

2024年 农历7月初7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值