C#农历日期

日期处理,是一个老生常谈的话题,几乎所有项目都需要涉及,要求也许不一样,有的需要计算周数,有的需要农历,有的需要计算节日……

本人网上看了一些相关帖子,结合本人思路用C#将其重新封装,思路如下:

农历与公历记年:

农历年一般都没有标准的365天左右,就是说农历年每年都会与公历年相差一段时间,累计起来之后,很可能出现农历年的夏天,刚好碰上公历年的冬天,这样两种计年法就出现混乱,所以为了解决问题,农历有闰月的概念,就是连续出现过两个农历同月(如1982年有两个农历四月,2004年有两个农历二月)。

*农历记年偏差都会以天为单位,闰年没有规律,一般闰月就是多一个月

*公历跟太阳运转偏差精确到分钟,所以一般公历每4年会出现一个闰年,在2月多一天,平衡偏差

如何计算两种纪年法:

   公历日期都有公式可以使用,所以很多开发语言都有封装好的类来完成计算等,但是公历到农历的转换就得自己想办法了,一般都是将一段时间的农历时间存入一个固定表格中,需要的时候进行查询,计算公历日期与某一个时间点的偏差

   例如:公历1900年2月1日就是1900年农历春节,假如我们以1900.1.31为基点,将1900-2050年农历年份信息存入lunarYear[]中,一个给定日期,计算到1900年的年数,就可以得到lunarYear[i]中的值,农历年信息,月份大小,阴历每月只能是2930天,一年用12(或13)个二进制位表示,对应位为130天,否则为29天,压缩为16进制存入表中。根据给出公历日期得到农历年份,一天等于1000*24*24*60 = 86400000毫秒,根据偏差计算农历月份(包括闰月)、日期等等.

 

其他信息

 农历干支、生肖一样都存入固定数组中,干支是(10*12/2 = 60年一个轮回,知道1900年后,其余年份可以直接就算出来,同理干支月、日也可以计算出来。

 24节气是公历计法,一般不会变化“每月两节气定,上半年六廿一,下半年八廿三,最多相差一两天”,也可根据偏差毫秒来计算。

4         星座、节日都是有固定日期,我们可以存入之后进行对比得到某个日期日期是否是节气日,国外父亲节、母亲节都是按某月第几周、周几计算,根据公历日期可以直接得到该月第几周的周几。

 

完成类:

using System;

using System.Collections.Generic;

using System.Text;

namespace HocyDate

{

   class LunarDateHocy

   {

     #region //构造方法

     public LunarDateHocy()

     {

     }

    //构造方法

    public LunarDateHocy(DateTime dt)

    {

       switch(this.CaculateWeekDay(dt.Year, dt.Month, dt.Day))

             {

           case 7:

               this.weekValue = "日";

               break;

           case 1:

               this.weekValue = "一";

               break;

           case 2:

               this.weekValue = "二";

               break;

           case 3 :

               this.weekValue = "三";

               break;

           case 4:

               this.weekValue = "四";

               break;

           case 5:

               this.weekValue = "五";

               break;

           case 6:

               this.weekValue = "六";

               break;

           

              }

         

          this.weekNumValue = this.returnweekNum(dt.Year, dt.Month , dt.Day).ToString();

          this.feteValue = this.Fete(dt.Month, dt.Day);

                   

           TimeSpan timeSpan = dt - baseDate;

           int offset = Convert.ToInt32(timeSpan.TotalDays); //86400000=1000*24*60*60

           int temp = 0;

           int i, lunarYear = 0, leap = 0, lunarMonth, lunarDay;//农历年

           bool isLeap;

           for (i = 1900; i < 2050 && offset > 0; i++)

           {

               temp = LunarYearDays(i);

               offset -= temp;

           }

           if (offset < 0)

           {

               offset += temp;

               i--;

           }

           lunarYear = i;

           leap = leapMonth(i); //闰哪个月

           isLeap = false;

           for (i = 1; i < 13 && offset > 0; i++)

           {

               //闰月

               if (leap > 0 && i == (leap + 1) && isLeap == false)

               {

                   --i;

                   isLeap = true;

                   temp = leapDays(lunarYear);

               }

               else

               {

                   temp = monthDays(lunarYear, i);

               }

               //解除闰月

               if (isLeap == true && i == (leap + 1))

               {

                   isLeap = false;

               }

               offset -= temp;

           }

           if (offset == 0 && leap > 0 && i == leap + 1)

               if (isLeap)

               {

                   isLeap = false;

               }

               else

               {

                   isLeap = true;

                   --i;

               }

           if (offset < 0)

           {

               offset += temp;

               --i;

           }

           lunarMonth = i;

           lunarDay = offset + 1;

          this.lunarFeteValue = LunarFete(lunarMonth, lunarDay);

          this.lunarGanZhiValue = Cyclical(lunarYear);

          this.lunarShengXiaoValue = Animal(lunarYear);

          this.lunarYearValue = FormatYear(lunarYear);

          this.lunarDayValue = cDay(lunarDay);

          if (isLeap == true)

          {

              this.lunarMonthValue = "闰" + lunarMonthName[lunarMonth-1];

          }

          else

          {

              this.lunarMonthValue = lunarMonthName[lunarMonth-1];

          }

       

      }

     #endregion

     #region私有成员 属性

     private DateTime baseDate = new DateTime(1900, 1, 31);

     private const ushort START_YEAR = 1901;

     private const ushort END_YEAR = 2050;

     private string lYear;

     private string lMonth;

     private string lday;

     private string lganZhi;

     private string lshengXiao;

     private string week;

     private string weekNum;

     private string fete;

     private string lunarFete;

     private string wordFete;

   

     public string lunarYearValue //获得农历年  中文

     {

        getreturn lYear;}

        set{ lYear = value;}

     }

       public string lunarMonthValue //获得农历年  中文

     {

         get { return lMonth; }

         set { lMonth = value; }

     }

       public string lunarDayValue //获得农历年  中文

     {

          get { return lday; }

          set { lday = value; }

      }

       public string lunarGanZhiValue //获得农历年  中文

      {

           get { return lganZhi; }

           set { lganZhi = value; }

       }

       public string lunarShengXiaoValue //获得农历年  中文

       {

           get { return lshengXiao; }

           set { lshengXiao = value; }

       }

       public string weekValue //获得星期  中文

       {

           get { return week; }

           set { week = value; }

       }

       public string weekNumValue //获得月内的第几周  中文

       {

           get { return weekNum; }

           set { weekNum = value; }

       }

       public string feteValue //获得公历节日  中文

       {

           get { return fete; }

           set { fete = value; }

       }

       public string lunarFeteValue //获得农历节日  中文

       {

           get { return lunarFete; }

           set { lunarFete = value; }

       }

       public string wordFeteValue //获得世界节日  中文

       {

           get { return wordFete; }

           set { wordFete = value; }

       }

      #endregion

     #region //静态数据

       private static int[] lunarInfo = { 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 static int[]  solarMonth={31,28,31,30,31,30,31,31,30,31,30,31};

    

      private static string[] Gan={"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};

      private static string[] Zhi={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};

      private static string[] Animals={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};

      private static string[] solarTerm = {"小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"};

     

      private static int[] sTermInfo = {0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758};

     

      private static string[] nStr1 = {"日","一","二","三","四","五","六","七","八","九","十"};

       private static string[] lunarYearName = { "零""一""二""三""四""五""六""七""八""九""十" };

      private static string[] nStr2 = {"初","十","廿","卅","□"};

      private static string[] monthName = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};

       private static string[] lunarMonthName = { "正月""二月""三月""四月""五月""六月""七月""八月""九月""十月""十一""腊月" };

     //国历节日*表示放假日

      private static string[] sFtv = {"0101*元旦","0202 世界湿地日","0210 国际气象节","0214 情人节","0301 国际海豹日","0303 全国爱耳日",

                                      "0305 学雷锋纪念日","0308 妇女节","0312 植树节孙中山逝世纪念日","0314 国际警察日","0315 消费者权益日",

                                      "0317 中国国医节国际航海日","0321 世界森林日消除种族歧视国际日世界儿歌日","0322 世界水日",

                                      "0323 世界气象日","0324 世界防治结核病日","0330 巴勒斯坦国土日","0401 愚人节全国爱国卫生运动月(四月) 税收宣传月(四月)",

                                     "0407 世界卫生日",

                                     "0422 世界地球日",

                                     "0423 世界图书和版权日",

                                     "0424 亚非新闻工作者日",

                                     "0501*劳动节",

                                     "0502*劳动节假日",

                                     "0503*劳动节假日",

                                     "0504 青年节",

                                     "0505 碘缺乏病防治日",

                                     "0508 世界红十字日",

                                     "0512 国际护士节",

                                     "0515 国际家庭日",

                                     "0517 国际电信日",

                                     "0518 国际博物馆日",

                                     "0520 全国学生营养日",

                                     "0523 国际牛奶日",

                                     "0531 世界无烟日",

                                     "0601 国际儿童节",

                                     "0605 世界环境保护日",

                                     "0606 全国爱眼日",

                                     "0617 防治荒漠化和干旱日",

                                     "0623 国际奥林匹克日",

                                     "0625 全国土地日",

                                     "0626 国际禁毒日",

                                     "0701 香港回归纪念日中共诞辰世界建筑日",

                                     "0702 国际体育记者日",

                                     "0707 抗日战争纪念日",

                                     "0711 世界人口日",

                                     "0730 非洲妇女日",

                                     "0801 建军节",

                                     "0808 中国男子节(爸爸节)",

                                     "0815 抗日战争胜利纪念",

                                     "0903 中国抗日战争胜利纪念日",

                                     "0908 国际扫盲日国际新闻工作者日",

                                     "0909 ***逝世纪念",

                                     "0910 中国教师节",

                                     "0914 世界清洁地球日",

                                     "0916 国际臭氧层保护日",

                                     "0918 九·一八事变纪念日",

                                     "0920 国际爱牙日",

                                     "0927 世界旅游日",

                                     "0928 孔子诞辰",

                                     "1001*国庆节世界音乐日国际老人节",

                                     "1002*国庆节假日国际和平与民主自由斗争日",

                                     "1003*国庆节假日",

                                     "1004 世界动物日",

                                     "1006 老人节",

                                     "1008 全国高血压日世界视觉日",

                                     "1009 世界邮政日万国邮联日",

                                     "1010 辛亥革命纪念日世界精神卫生日",

                                     "1013 世界保健日国际教师节",

                                     "1014 世界标准日",

                                     "1015 国际盲人节(白手杖节)",

                                     "1016 世界粮食日",

                                     "1017 世界消除贫困日",

                                     "1022 世界传统医药日",

                                     "1024 联合国日",

                                     "1031 世界勤俭日",

                                     "1107 十月社会主义革命纪念日",

                                     "1108 中国记者日",

                                     "1109 全国消防安全宣传教育日",

                                     "1110 世界青年节",

                                     "1111 国际科学与和平周(本日所属的一周)",

                                     "1112 孙中山诞辰纪念日",

                                     "1114 世界糖尿病日",

                                     "1117 国际大学生节世界学生节",

                                     "1120*彝族年",

                                     "1121*彝族年世界问候日世界电视日",

                                     "1122*彝族年",

                                     "1129 国际声援巴勒斯坦人民国际日",

                                     "1201 世界艾滋病日",

                                     "1203 世界残疾人日",

                                     "1205 国际经济和社会发展志愿人员日",

                                     "1208 国际儿童电视日",

                                     "1209 世界足球日",

                                     "1210 世界人权日",

                                     "1212 西安事变纪念日",

                                     "1213 南京大屠杀(1937年)纪念日!紧记血泪史!",

                                     "1220 澳门回归纪念",

                                     "1221 国际篮球日",

                                     "1224 平安夜",

                                     "1225 圣诞节",

                                     "1226 ***诞辰纪念"

                                     };

                                     //农历节日*表示放假日

     private static string[] lFtv = {

                                     "0101*春节",

                                     "0102*春节",

                                     "0103*春节",

                                     "0115 元宵节",

                                     "0505 端午节",

                                     "0624*火把节",

                                     "0625*火把节",

                                     "0626*火把节",

                                     "0707 七夕情人节",

                                     "0715 中元节",

                                     "0815 中秋节",

                                     "0909 重阳节",

                                     "1208 腊八节",

                                     "1224 小年",

                                     "0100 除夕"

                                      };

    //某月的第几个星期几

     private static string[] twFtv = {

                                     "0150 世界麻风日"//一月的最后一个星期日(月倒数第一个星期日)

                                     "0351 全国中小学生安全教育日",

                                     "0520 国际母亲节",

                                     "0530 全国助残日",

                                    "0630 父亲节",

                                     "0730 被奴役国家周",

                                     "0932 国际和平日",

                                     "0940 国际聋人节世界儿童日",

                                     "0950 世界海事日",

                                     "1011 国际住房日",

                                     "1013 国际减轻自然灾害日(减灾日)",

                                     "1144 感恩节"

                                   };

       #endregion

     #region//日期计算方法

     ///

     ///传入农历年 返回农历y年的总天数

     ///

     ///

     ///

    public int LunarYearDays(int year) //====================================== 返回农历y年的总天数

    {

        int i, sum = 348;

        for(i=0x8000; i>0x8; i>>=1)

        {

            sum += ((lunarInfo[year-1900] & i)!=0)? 1: 0;

        }

        return(sum+leapDays(year));

     }

   ///

     ///返回农历y年闰月的天数

   ///

   ///

   ///

   public int leapDays(int year) //====================================== 返回农历y年闰月的天数

    {

        if (leapMonth(year)!=0) 

         

            return (((lunarInfo[year-1900] & 0x10000)!=0)? 30: 29);

       else

       

            return(0);

    }

    ///

     ///返回农历y年闰哪个月1-12 , 没闰返回0

    ///

    ///

    ///

    public int leapMonth(int year) //====================================== 返回农历y年闰哪个月1-12 , 没闰返回0

    {

        return (lunarInfo[year - 1900] & 0xf);

    }

    ///

     ///返回农历y年m月的总天数

    ///

    ///

    ///

    ///

    public int monthDays(int year, int month)//====================================== 返回农历y年m月的总天数

    {

        return (((lunarInfo[year - 1900] & (0x10000 >> month))!=0) ? 30 : 29);

    }

   

    ///

    ///返回公历y年某m+1月的天数

   ///

    ///公历年

    ///公历月

    ///

    public int solarDays(int y, int m)

  {

          if (m == 1)

            return (((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) ? 29 : 28);

          else

            return (solarMonth[m]);

    }

   

   ///

    ///传入 传入农历年返回干支, 0=甲子

   ///

   ///

   ///

   public string Cyclical(int lunarYear)

    {

        return (Gan[(lunarYear - 4) % 60 % 10] + Zhi[(lunarYear - 4) % 60 % 12]);

    }

    ///

 

    ///传入农历年返回干支, 0=鼠

    ///

   ///

    ///

    private string Animal(int lunarYear)

    {

        return Animals[(lunarYear - 4) % 60 % 12];

    }

    private static string cDay(int d)

    {

         string s;

         switch (d)

         {

            case 10:

              s = "初十";

              break;

            case 20:

              s ="二十";

               break;

            case 30:

               s = "三十";

               break;

            default :

              s = nStr2[d/10];

              s += nStr1[d%10];

              break;

         }

          return(s);

    }

   ///

   ///格式化中文年

   ///

   ///

   ///

    private static string FormatYear(int y)

    {

       

        int sValue = y / 1000;

        int hValue = (y - sValue * 1000) / 100;

        int tenValue = (y - (sValue * 1000 + hValue * 100)) / 10;

        int gValue = y - sValue * 1000 - hValue * 100 - tenValue * 10;

        return lunarYearName[sValue] + lunarYearName[hValue] + lunarYearName[tenValue] + lunarYearName[gValue];

     }

     #endregion

     #region直接格式化日期 返回农历日期长字符串

     ///

    ///根据公历日期返回农历日期

    ///

    ///

    ///

    public string getLunarDate(DateTime dt)

    {

     

      TimeSpan timeSpan = dt - baseDate;

      int offset =Convert.ToInt32(timeSpan.TotalDays); //86400000=1000*24*60*60

      int temp = 0;

      int i, lunarYear = 0, leap = 0, lunarMonth, lunarDay;//农历年

      bool isLeap;

      for (i = 1900; i < 2050 && offset > 0; i++)

      {

          temp = LunarYearDays(i);

          offset -= temp;

      }

      if (offset < 0)

      {

          offset += temp;

          i--;

      }

     lunarYear= i;

     leap = leapMonth(i); //闰哪个月

     isLeap = false;

     for (i = 1; i < 13 && offset > 0; i++)

     {

           //闰月

         if (leap > 0 && i == (leap + 1) && isLeap == false)

         {

             --i;

             isLeap = true;

             temp = leapDays(lunarYear);

         }

         else

         {

             temp = monthDays(lunarYear, i);

         }

         //解除闰月

         if (isLeap == true && i == (leap + 1))

         {

             isLeap = false;

         }

        offset -= temp;

      }

        if (offset == 0 && leap > 0 && i == leap + 1)

            if (isLeap)

            {

                isLeap = false;

            }

            else

            {

                isLeap = true;

                --i;

            }

        if (offset < 0)

        {

            offset += temp;

            --i;

        }

        lunarMonth = i;

        lunarDay = offset + 1;

        string GanZHi = Cyclical(lunarYear);

        string animal = Animal(lunarYear);

        string lfete = LunarFete(lunarMonth, lunarDay);

        if (isLeap == true)

        {

            return FormatYear(lunarYear) + "年闰" + lunarMonthName[lunarMonth-1] + cDay(lunarDay) + "#" + GanZHi + "#" + animal+"年"+" "+lfete;

        }

        else

        {

            return FormatYear(lunarYear) + "年" + lunarMonthName[lunarMonth - 1] + cDay(lunarDay) + "#" + GanZHi + "#" + animal + "年" + " " + lfete;

        }

    }

    #endregion

     #region节假日计算

    ///

    ///公历日期返回公历节假日

    ///

   ///

   ///

   ///

   public string Fete(int month,int day)

   {

       string str = @"("d{2})("d{2})(["s"*])(.+)$"//匹配的正则表达式

       System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(str) ;

       for (int i = 0; i < sFtv.Length; i++)

       {

           string[] s = re.Split(sFtv[i]);

           if (Convert.ToInt32(s[1]) == month && Convert.ToInt32(s[2]) == day)

           {

              

               return s[4];

           }

       }

       return "";   

 

   }

    ///

    ///农历日期返回农历节日

    ///

    ///

    ///

    ///

    public string LunarFete(int lunarMonth, int lunarDay)

    {

           string str = @"("d{2})("d{2})(["s"*])(.+)$"//匹配的正则表达式

           System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(str);

           for (int i = 0; i < lFtv.Length; i++)

           {

               string[] s = re.Split(lFtv[i]);

               if (Convert.ToInt32(s[1]) == lunarMonth && Convert.ToInt32(s[2]) == lunarDay)

               {

                   return s[4];

               }

           }

           return "";

    }

       ///

       ///

       ///

       ///

       ///该月第几周

     /// 周几

       ///

       public string WordFete(int month, int num,int week)

       {

           string str = @"("d{2})("d{1})("d{1})(["s"*])(.+)$"//匹配的正则表达式

           System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(str);

           for (int i = 0; i < twFtv.Length; i++)

           {

               string[] s = re.Split(twFtv[i]);

               if (Convert.ToInt32(s[1]) == month && Convert.ToInt32(s[2]) == num && Convert.ToInt32(s[2]) == week)

               {

                   return s[5];

               }

           }

           return "";

       }

       ///

       ///期根据年月日计算星期几方法 返回int

       ///

       ///

       ///

       ///

       ///

       public int CaculateWeekDay(int y, int m, int d) //y-年,m-月,d-日,期根据年月日计算星期几的函数

       {

           if (m == 1)

           {

               m = 13;

               y = y - 1;

           }

           if (m == 2)

           {

               m = 14;

               y = y - 1;

           }

           int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; //基姆拉尔森计算公式

           int weekstr=0;

           switch (week)

           {

               case 0: weekstr = 1; break;

               case 1: weekstr = 2; break;

               case 2: weekstr = 3; break;

               case 3: weekstr = 4; break;

               case 4: weekstr = 5; break;

               case 5: weekstr = 6; break;

               case 6: weekstr = 7; break;

           }

           return weekstr;

       }

       ///

       ///计算是该月第几周

       ///

       ///

       ///

       ///

       ///

       public int returnweekNum(int y, int m, int d)

       {

           int weekNo = 0;

           int week = this.CaculateWeekDay(y, m, d);

           if (week == 7)

               week = 0;

           if (d > (week))

           {

               if ((d - week) % 7 == 0)

                   weekNo = (d - week) / 7 + 1;

               else

                   weekNo = (d - week) / 7 + 2;

           }

           else

           {

               weekNo = 1;

           }

           return weekNo;

       }

       ///

       ///计算节气

       ///

       ///

    /* public string l_GetLunarHolDay(int iYear, int num)

     {

         double sTermYearLen = 31556925974.7,offDate;

         offDate = (sTermYearLen * (iYear - 1900)) + sTermInfo[num]*600000;

         DateTime dateBase = new DateTime(1900, 1, 6, 2, 5,0);

         //DateTime dt = new DateTime(Convert.ToUInt32(offDate));

         return offDate.ToString();     

         // var offDate = new Date( ( 31556925974.7*(y-1900) + sTermInfo[n]*60000 ) + Date.UTC(1900,0,6,2,5) );

       

         // return(offDate.getUTCDate());

      }*/

    #endregion

  }

}

调用示例:

     static void Main(string[] args)

        {

            DateTime dtDay = new DateTime(1982,10,1);         

            LunarDateHocy ldh = new LunarDateHocy(dtDay);

            Console.WriteLine(ldh.lunarYearValue);

            Console.WriteLine(ldh.lunarGanZhiValue);

            Console.WriteLine(ldh.lunarMonthValue);

            Console.WriteLine(ldh.lunarDayValue);

            Console.WriteLine(ldh.lunarShengXiaoValue);

            Console.WriteLine(ldh.feteValue);

            Console.WriteLine(ldh.lunarFeteValue);

            Console.WriteLine(""+ldh.weekNumValue+"");

Console.WriteLine(""+ldh.weekValue);

            Console.WriteLine("--------------------------------------------------------------------");

            Console.WriteLine(ldh.getLunarDate(dtDay));

            Console.Write(ldh.l_GetLunarHolDay(2007,20));

            Console.Read();

        }

分类:  2..NET新手区, C#|控件
好文要顶  关注我  收藏该文   
蓝天旭日
关注 - 0
粉丝 - 28
+加关注
3
0
» 下一篇: ajax使用心得

Feedback

#1楼   

2007-11-16 15:18 by  gjz
LZ 代码里面 匹配的正则表达式 好像有误的,编译不能通过
支持(0) 反对(0)

#2楼   

2007-11-16 16:40 by  可以通过!
没有哦,在vs2005里面可以通过!
支持(0) 反对(0)

#3楼   

2007-11-17 09:14 by  gjz
string str = @"("d{2})("d{2})(["s"*])(.+)$"; //匹配的正则表达式
string str = @"("d{2})("d{1})("d{1})(["s"*])(.+)$"; //匹配的正则表达式

在.net 2k5编译 那些双引号有歧义
支持(0) 反对(0)

#4楼   

2007-11-19 11:10 by  其实这个表达式的作用是分离成数组
如 :1013* 世界保健日国际教师节 
string str = @"("d{2})("d{2})(["s"*])(.+)$"; 
将其分离成"10" "13" "*" "世界保健日国际教师节" 的数组 
方法很多,直接用string的sqilt也行,不过这个表达式博主到是运行过才帖出来的 
支持(0) 反对(0)

#5楼[楼主]   

2008-01-17 09:50 by  蓝天旭日   
应哥们要求,改动如下函数可以得到月干支和日干支 
public string getLunarDate(DateTime dt) 



TimeSpan timeSpan = dt - baseDate; 

int offset =Convert.ToInt32(timeSpan.TotalDays); //86400000=1000*24*60*60 

int gzMonthOffset = 14; //定义月偏移量 
int gzDayOffset =offset + 40;//定义日偏移量 
//基本日期标准都是1900-1-31 

int temp = 0; 

int i, lunarYear = 0, leap = 0, lunarMonth, lunarDay;//农历年 

bool isLeap; 

for (i = 1900; i < 2050 && offset > 0; i++) 

temp = LunarYearDays(i); 
offset -= temp; 
gzMonthOffset += 12; 

if (offset < 0) 

offset += temp; 
i--; 
gzMonthOffset -= 12; 



lunarYear= i; 

leap = leapMonth(i); //闰哪个月 

isLeap = false; 

for (i = 1; i < 13 && offset > 0; i++) 

//闰月 
if (leap > 0 && i == (leap + 1) && isLeap == false) 

--i; 

isLeap = true; 

temp = leapDays(lunarYear); 

else 

temp = monthDays(lunarYear, i); 


//解除闰月 
/* 
if(this.isLeap==true && i==(leap+1)) 
this.isLeap = false 
offset -= temp 
if(this.isLeap == false) 
* this.monCyl ++ 

*/ 
if (isLeap == true && i == (leap + 1)) 

isLeap = false; 

offset -= temp; 
if (isLeap == false) 
gzMonthOffset++; 




if (offset == 0 && leap > 0 && i == leap + 1) 
if (isLeap) 

isLeap = false; 

else 

isLeap = true; 
--i; 
gzMonthOffset--; 


if (offset < 0) 

offset += temp; 
--i; 
gzMonthOffset--; 


lunarMonth = i; 

lunarDay = offset + 1; 

string GanZHi = Cyclical(lunarYear); 

string animal = Animal(lunarYear); 

string lfete = LunarFete(lunarMonth, lunarDay); 

if (isLeap == true) 


string s = FormatYear(lunarYear) + "年闰" + lunarMonthName[lunarMonth - 1] + cDay(lunarDay) + "#" + animal + "年" + " " + lfete+"-------"; 
s += GanZHi+"年"+CyclicalMonthorDay(gzMonthOffset) + "月" +CyclicalMonthorDay(gzDayOffset) + "日"; 
return s; 

else 

string s1 = FormatYear(lunarYear) + "年" + lunarMonthName[lunarMonth - 1] + cDay(lunarDay) + "#" + animal + "年" + " " + lfete+"---------"; 
s1 += GanZHi + "年" + CyclicalMonthorDay(gzMonthOffset) + "月" + CyclicalMonthorDay(gzDayOffset) + "日"; 
return s1; 





增加一个方法: 
//根据偏移量计算日干支或者月干支 num=偏移量 
public string CyclicalMonthorDay(int num) 

return (Gan[num % 10] + Zhi[num % 12]); 
}

你可能感兴趣的:(C#学习)