四联光电智能照明论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3254|回复: 0
打印 上一主题 下一主题

C#实现万年历(农历、节气、节日、星座、星宿、属相、生肖、闰年月、时辰)

[复制链接]
  • TA的每日心情
    开心
    2022-6-10 09:59
  • 366

    主题

    741

    帖子

    9649

    积分

    超级版主

    Rank: 8Rank: 8

    积分
    9649
    跳转到指定楼层
    楼主
    发表于 2016-11-4 10:44:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 gxm771208 于 2016-11-4 10:46 编辑

    C# 万年历 农历 节气 节日 星座 星宿 属相 生肖 闰年月 时辰
    地址:http://www.cnblogs.com/txw1958/a ... sharp-calendar.html



    1. using System.Collections.Generic;
    2. using System.Text;


    3. using System;

    4. namespace yangliToyinli
    5. {
    6.     #region ChineseCalendarException
    7.     /// <summary>
    8.     /// 中国日历异常处理
    9.     /// </summary>
    10.     public class ChineseCalendarException : System.Exception
    11.     {
    12.         public ChineseCalendarException(string msg)
    13.             : base(msg)
    14.         {
    15.         }
    16.     }

    17.     #endregion

    18.     /// <summary>
    19.     /// 中国农历类 版本V1.0 支持 1900.1.31日起至 2049.12.31日止的数据
    20.     /// </summary>
    21.     /// <remarks>
    22.     /// 本程序使用数据来源于网上的万年历查询,并综合了一些其它数据
    23.     /// </remarks>
    24.     public class ChineseCalendar
    25.     {
    26.         #region 内部结构
    27.         private struct SolarHolidayStruct
    28.         {
    29.             public int Month;
    30.             public int Day;
    31.             public int Recess; //假期长度
    32.             public string HolidayName;
    33.             public SolarHolidayStruct(int month, int day, int recess, string name)
    34.             {
    35.                 Month = month;
    36.                 Day = day;
    37.                 Recess = recess;
    38.                 HolidayName = name;
    39.             }
    40.         }

    41.         private struct LunarHolidayStruct
    42.         {
    43.             public int Month;
    44.             public int Day;
    45.             public int Recess;
    46.             public string HolidayName;

    47.             public LunarHolidayStruct(int month, int day, int recess, string name)
    48.             {
    49.                 Month = month;
    50.                 Day = day;
    51.                 Recess = recess;
    52.                 HolidayName = name;
    53.             }
    54.         }

    55.         private struct WeekHolidayStruct
    56.         {
    57.             public int Month;
    58.             public int WeekAtMonth;
    59.             public int WeekDay;
    60.             public string HolidayName;

    61.             public WeekHolidayStruct(int month, int weekAtMonth, int weekDay, string name)
    62.             {
    63.                 Month = month;
    64.                 WeekAtMonth = weekAtMonth;
    65.                 WeekDay = weekDay;
    66.                 HolidayName = name;
    67.             }
    68.         }
    69.         #endregion

    70.         #region 内部变量
    71.         private DateTime _date;
    72.         private DateTime _datetime;

    73.         private int _cYear;
    74.         private int _cMonth;
    75.         private int _cDay;
    76.         private bool _cIsLeapMonth; //当月是否闰月
    77.         private bool _cIsLeapYear; //当年是否有闰月
    78.         #endregion

    79.         #region 基础数据
    80.         #region 基本常量
    81.         private const int MinYear = 1900;
    82.         private const int MaxYear = 2050;
    83.         private static DateTime MinDay = new DateTime(1900, 1, 30);
    84.         private static DateTime MaxDay = new DateTime(2049, 12, 31);
    85.         private const int GanZhiStartYear = 1864; //干支计算起始年
    86.         private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22);//起始日
    87.         private const string HZNum = "零一二三四五六七八九";
    88.         private const int AnimalStartYear = 1900; //1900年为鼠年
    89.         private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);//28星宿参考值,本日为角
    90.         #endregion

    91.         #region 阴历数据
    92.         /// <summary>
    93.         /// 来源于网上的农历数据
    94.         /// </summary>
    95.         /// <remarks>
    96.         /// 数据结构如下,共使用17位数据
    97.         /// 第17位:表示闰月天数,0表示29天   1表示30天
    98.         /// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
    99.         /// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
    100.         ///</remarks>
    101.         private static int[] LunarDateArray = new int[]{
    102.                 0x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
    103.                 0x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
    104.                 0x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
    105.                 0x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
    106.                 0x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
    107.                 0x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
    108.                 0x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
    109.                 0x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
    110.                 0x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
    111.                 0x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
    112.                 0x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
    113.                 0x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
    114.                 0x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
    115.                 0x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
    116.                 0x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
    117.                 0x14B63        
    118.                 };

    119.         #endregion

    120.         #region 星座名称
    121.         private static string[] _constellationName =
    122.                 {
    123.                     "白羊座", "金牛座", "双子座",
    124.                     "巨蟹座", "狮子座", "处女座",
    125.                     "天秤座", "天蝎座", "射手座",
    126.                     "摩羯座", "水瓶座", "双鱼座"
    127.                 };
    128.         #endregion

    129.         #region 二十四节气
    130.         private static string[] _lunarHolidayName =
    131.                     {
    132.                     "小寒", "大寒", "立春", "雨水",
    133.                     "惊蛰", "春分", "清明", "谷雨",
    134.                     "立夏", "小满", "芒种", "夏至",
    135.                     "小暑", "大暑", "立秋", "处暑",
    136.                     "白露", "秋分", "寒露", "霜降",
    137.                     "立冬", "小雪", "大雪", "冬至"
    138.                     };
    139.         #endregion

    140.         #region 二十八星宿
    141.         private static string[] _chineseConstellationName =
    142.             {
    143.                   //四        五      六         日        一      二      三  
    144.                 "角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹",
    145.                 "斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝",
    146.                 "奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿",
    147.                 "井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","轸水蚓"
    148.             };
    149.         #endregion

    150.         #region 节气数据
    151.         private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
    152.         private static int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 };
    153.         #endregion

    154.         #region 农历相关数据
    155.         private static string ganStr = "甲乙丙丁戊己庚辛壬癸";
    156.         private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥";
    157.         private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
    158.         private static string nStr1 = "日一二三四五六七八九";
    159.         private static string nStr2 = "初十廿卅";
    160.         private static string[] _monthString =
    161.                 {
    162.                     "出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"
    163.                 };
    164.         #endregion

    165.         #region 按公历计算的节日
    166.         private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{
    167.             new SolarHolidayStruct(1, 1, 1, "元旦"),
    168.             new SolarHolidayStruct(2, 2, 0, "世界湿地日"),
    169.             new SolarHolidayStruct(2, 10, 0, "国际气象节"),
    170.             new SolarHolidayStruct(2, 14, 0, "情人节"),
    171.             new SolarHolidayStruct(3, 1, 0, "国际海豹日"),
    172.             new SolarHolidayStruct(3, 5, 0, "学雷锋纪念日"),
    173.             new SolarHolidayStruct(3, 8, 0, "妇女节"),
    174.             new SolarHolidayStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),
    175.             new SolarHolidayStruct(3, 14, 0, "国际警察日"),
    176.             new SolarHolidayStruct(3, 15, 0, "消费者权益日"),
    177.             new SolarHolidayStruct(3, 17, 0, "中国国医节 国际航海日"),
    178.             new SolarHolidayStruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日"),
    179.             new SolarHolidayStruct(3, 22, 0, "世界水日"),
    180.             new SolarHolidayStruct(3, 24, 0, "世界防治结核病日"),
    181.             new SolarHolidayStruct(4, 1, 0, "愚人节"),
    182.             new SolarHolidayStruct(4, 7, 0, "世界卫生日"),
    183.             new SolarHolidayStruct(4, 22, 0, "世界地球日"),
    184.             new SolarHolidayStruct(5, 1, 1, "劳动节"),
    185.             new SolarHolidayStruct(5, 2, 1, "劳动节假日"),
    186.             new SolarHolidayStruct(5, 3, 1, "劳动节假日"),
    187.             new SolarHolidayStruct(5, 4, 0, "青年节"),
    188.             new SolarHolidayStruct(5, 8, 0, "世界红十字日"),
    189.             new SolarHolidayStruct(5, 12, 0, "国际护士节"),
    190.             new SolarHolidayStruct(5, 31, 0, "世界无烟日"),
    191.             new SolarHolidayStruct(6, 1, 0, "国际儿童节"),
    192.             new SolarHolidayStruct(6, 5, 0, "世界环境保护日"),
    193.             new SolarHolidayStruct(6, 26, 0, "国际禁毒日"),
    194.             new SolarHolidayStruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日"),
    195.             new SolarHolidayStruct(7, 11, 0, "世界人口日"),
    196.             new SolarHolidayStruct(8, 1, 0, "建军节"),
    197.             new SolarHolidayStruct(8, 8, 0, "中国男子节 父亲节"),
    198.             new SolarHolidayStruct(8, 15, 0, "抗日战争胜利纪念"),
    199.             new SolarHolidayStruct(9, 9, 0, "  逝世纪念"),
    200.             new SolarHolidayStruct(9, 10, 0, "教师节"),
    201.             new SolarHolidayStruct(9, 18, 0, "九·一八事变纪念日"),
    202.             new SolarHolidayStruct(9, 20, 0, "国际爱牙日"),
    203.             new SolarHolidayStruct(9, 27, 0, "世界旅游日"),
    204.             new SolarHolidayStruct(9, 28, 0, "孔子诞辰"),
    205.             new SolarHolidayStruct(10, 1, 1, "国庆节 国际音乐日"),
    206.             new SolarHolidayStruct(10, 2, 1, "国庆节假日"),
    207.             new SolarHolidayStruct(10, 3, 1, "国庆节假日"),
    208.             new SolarHolidayStruct(10, 6, 0, "老人节"),
    209.             new SolarHolidayStruct(10, 24, 0, "联合国日"),
    210.             new SolarHolidayStruct(11, 10, 0, "世界青年节"),
    211.             new SolarHolidayStruct(11, 12, 0, "孙中山诞辰纪念"),
    212.             new SolarHolidayStruct(12, 1, 0, "世界艾滋病日"),
    213.             new SolarHolidayStruct(12, 3, 0, "世界残疾人日"),
    214.             new SolarHolidayStruct(12, 20, 0, "澳门回归纪念"),
    215.             new SolarHolidayStruct(12, 24, 0, "平安夜"),
    216.             new SolarHolidayStruct(12, 25, 0, "圣诞节"),
    217.             new SolarHolidayStruct(12, 26, 0, " 诞辰纪念")
    218.            };
    219.         #endregion

    220.         #region 按农历计算的节日
    221.         private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
    222.             new LunarHolidayStruct(1, 1, 1, "春节"),
    223.             new LunarHolidayStruct(1, 15, 0, "元宵节"),
    224.             new LunarHolidayStruct(5, 5, 0, "端午节"),
    225.             new LunarHolidayStruct(7, 7, 0, "七夕情人节"),
    226.             new LunarHolidayStruct(7, 15, 0, "中元节 盂兰盆节"),
    227.             new LunarHolidayStruct(8, 15, 0, "中秋节"),
    228.             new LunarHolidayStruct(9, 9, 0, "重阳节"),
    229.             new LunarHolidayStruct(12, 8, 0, "腊八节"),
    230.             new LunarHolidayStruct(12, 23, 0, "北方小年(扫房)"),
    231.             new LunarHolidayStruct(12, 24, 0, "南方小年(掸尘)"),
    232.             //new LunarHolidayStruct(12, 30, 0, "除夕")  //注意除夕需要其它方法进行计算
    233.         };
    234.         #endregion

    235.         #region 按某月第几个星期几
    236.         private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{
    237.             new WeekHolidayStruct(5, 2, 1, "母亲节"),
    238.             new WeekHolidayStruct(5, 3, 1, "全国助残日"),
    239.             new WeekHolidayStruct(6, 3, 1, "父亲节"),
    240.             new WeekHolidayStruct(9, 3, 3, "国际和平日"),
    241.             new WeekHolidayStruct(9, 4, 1, "国际聋人节"),
    242.             new WeekHolidayStruct(10, 1, 2, "国际住房日"),
    243.             new WeekHolidayStruct(10, 1, 4, "国际减轻自然灾害日"),
    244.             new WeekHolidayStruct(11, 4, 5, "感恩节")
    245.         };
    246.         #endregion

    247.         #endregion

    248.         #region 构造函数
    249.         #region ChinaCalendar <公历日期初始化>
    250.         /// <summary>
    251.         /// 用一个标准的公历日期来初使化
    252.         /// </summary>
    253.         /// <param name="dt"></param>
    254.         public ChineseCalendar(DateTime dt)
    255.         {
    256.             int i;
    257.             int leap;
    258.             int temp;
    259.             int offset;

    260.             CheckDateLimit(dt);

    261.             _date = dt.Date;
    262.             _datetime = dt;

    263.             //农历日期计算部分
    264.             leap = 0;
    265.             temp = 0;

    266.             TimeSpan ts = _date - ChineseCalendar.MinDay;//计算两天的基本差距
    267.             offset = ts.Days;

    268.             for (i = MinYear; i <= MaxYear; i++)
    269.             {
    270.                 temp = GetChineseYearDays(i);  //求当年农历年天数
    271.                 if (offset - temp < 1)
    272.                     break;
    273.                 else
    274.                 {
    275.                     offset = offset - temp;
    276.                 }
    277.             }
    278.             _cYear = i;

    279.             leap = GetChineseLeapMonth(_cYear);//计算该年闰哪个月
    280.             //设定当年是否有闰月
    281.             if (leap > 0)
    282.             {
    283.                 _cIsLeapYear = true;
    284.             }
    285.             else
    286.             {
    287.                 _cIsLeapYear = false;
    288.             }

    289.             _cIsLeapMonth = false;
    290.             for (i = 1; i <= 12; i++)
    291.             {
    292.                 //闰月
    293.                 if ((leap > 0) && (i == leap + 1) && (_cIsLeapMonth == false))
    294.                 {
    295.                     _cIsLeapMonth = true;
    296.                     i = i - 1;
    297.                     temp = GetChineseLeapMonthDays(_cYear); //计算闰月天数
    298.                 }
    299.                 else
    300.                 {
    301.                     _cIsLeapMonth = false;
    302.                     temp = GetChineseMonthDays(_cYear, i);//计算非闰月天数
    303.                 }

    304.                 offset = offset - temp;
    305.                 if (offset <= 0) break;
    306.             }

    307.             offset = offset + temp;
    308.             _cMonth = i;
    309.             _cDay = offset;
    310.         }
    311.         #endregion

    312.         #region ChinaCalendar <农历日期初始化>
    313.         /// <summary>
    314.         /// 用农历的日期来初使化
    315.         /// </summary>
    316.         /// <param name="cy">农历年</param>
    317.         /// <param name="cm">农历月</param>
    318.         /// <param name="cd">农历日</param>
    319.         /// <param name="LeapFlag">闰月标志</param>
    320.         public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
    321.         {
    322.             int i, leap, Temp, offset;

    323.             CheckChineseDateLimit(cy, cm, cd, leapMonthFlag);

    324.             _cYear = cy;
    325.             _cMonth = cm;
    326.             _cDay = cd;

    327.             offset = 0;

    328.             for (i = MinYear; i < cy; i++)
    329.             {
    330.                 Temp = GetChineseYearDays(i); //求当年农历年天数
    331.                 offset = offset + Temp;
    332.             }

    333.             leap = GetChineseLeapMonth(cy);// 计算该年应该闰哪个月
    334.             if (leap != 0)
    335.             {
    336.                 this._cIsLeapYear = true;
    337.             }
    338.             else
    339.             {
    340.                 this._cIsLeapYear = false;
    341.             }

    342.             if (cm != leap)
    343.             {
    344.                 _cIsLeapMonth = false;  //当前日期并非闰月
    345.             }
    346.             else
    347.             {
    348.                 _cIsLeapMonth = leapMonthFlag;  //使用用户输入的是否闰月月份
    349.             }


    350.             if ((_cIsLeapYear == false) || //当年没有闰月
    351.                  (cm < leap)) //计算月份小于闰月     
    352.             {
    353.                 #region ...
    354.                 for (i = 1; i < cm; i++)
    355.                 {
    356.                     Temp = GetChineseMonthDays(cy, i);//计算非闰月天数
    357.                     offset = offset + Temp;
    358.                 }

    359.                 //检查日期是否大于最大天
    360.                 if (cd > GetChineseMonthDays(cy, cm))
    361.                 {
    362.                     throw new ChineseCalendarException("不合法的农历日期");
    363.                 }
    364.                 offset = offset + cd; //加上当月的天数
    365.                 #endregion
    366.             }
    367.             else   //是闰年,且计算月份大于或等于闰月
    368.             {
    369.                 #region ...
    370.                 for (i = 1; i < cm; i++)
    371.                 {
    372.                     Temp = GetChineseMonthDays(cy, i); //计算非闰月天数
    373.                     offset = offset + Temp;
    374.                 }

    375.                 if (cm > leap) //计算月大于闰月
    376.                 {
    377.                     Temp = GetChineseLeapMonthDays(cy);   //计算闰月天数
    378.                     offset = offset + Temp;               //加上闰月天数

    379.                     if (cd > GetChineseMonthDays(cy, cm))
    380.                     {
    381.                         throw new ChineseCalendarException("不合法的农历日期");
    382.                     }
    383.                     offset = offset + cd;
    384.                 }
    385.                 else  //计算月等于闰月
    386.                 {
    387.                     //如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数
    388.                     if (this._cIsLeapMonth == true) //计算月为闰月
    389.                     {
    390.                         Temp = GetChineseMonthDays(cy, cm); //计算非闰月天数
    391.                         offset = offset + Temp;
    392.                     }

    393.                     if (cd > GetChineseLeapMonthDays(cy))
    394.                     {
    395.                         throw new ChineseCalendarException("不合法的农历日期");
    396.                     }
    397.                     offset = offset + cd;
    398.                 }
    399.                 #endregion
    400.             }


    401.             _date = MinDay.AddDays(offset);
    402.         }
    403.         #endregion
    404.         #endregion

    405.         #region 私有函数

    406.         #region GetChineseMonthDays
    407.         //传回农历 y年m月的总天数
    408.         private int GetChineseMonthDays(int year, int month)
    409.         {
    410.             if (BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), (16 - month)))
    411.             {
    412.                 return 30;
    413.             }
    414.             else
    415.             {
    416.                 return 29;
    417.             }
    418.         }
    419.         #endregion

    420.         #region GetChineseLeapMonth
    421.         //传回农历 y年闰哪个月 1-12 , 没闰传回 0
    422.         private int GetChineseLeapMonth(int year)
    423.         {

    424.             return LunarDateArray[year - MinYear] & 0xF;

    425.         }
    426.         #endregion

    427.         #region GetChineseLeapMonthDays
    428.         //传回农历 y年闰月的天数
    429.         private int GetChineseLeapMonthDays(int year)
    430.         {
    431.             if (GetChineseLeapMonth(year) != 0)
    432.             {
    433.                 if ((LunarDateArray[year - MinYear] & 0x10000) != 0)
    434.                 {
    435.                     return 30;
    436.                 }
    437.                 else
    438.                 {
    439.                     return 29;
    440.                 }
    441.             }
    442.             else
    443.             {
    444.                 return 0;
    445.             }
    446.         }
    447.         #endregion

    448.         #region GetChineseYearDays
    449.         /// <summary>
    450.         /// 取农历年一年的天数
    451.         /// </summary>
    452.         /// <param name="year"></param>
    453.         /// <returns></returns>
    454.         private int GetChineseYearDays(int year)
    455.         {
    456.             int i, f, sumDay, info;

    457.             sumDay = 348; //29天 X 12个月
    458.             i = 0x8000;
    459.             info = LunarDateArray[year - MinYear] & 0x0FFFF;

    460.             //计算12个月中有多少天为30天
    461.             for (int m = 0; m < 12; m++)
    462.             {
    463.                 f = info & i;
    464.                 if (f != 0)
    465.                 {
    466.                     sumDay++;
    467.                 }
    468.                 i = i >> 1;
    469.             }
    470.             return sumDay + GetChineseLeapMonthDays(year);
    471.         }
    472.         #endregion

    473.         #region GetChineseHour
    474.         /// <summary>
    475.         /// 获得当前时间的时辰
    476.         /// </summary>
    477.         /// <param name="time"></param>
    478.         /// <returns></returns>
    479.         ///
    480.         private string GetChineseHour(DateTime dt)
    481.         {

    482.             int _hour, _minute, offset, i;
    483.             int indexGan;
    484.             string ganHour, zhiHour;
    485.             string tmpGan;

    486.             //计算时辰的地支
    487.             _hour = dt.Hour;    //获得当前时间小时
    488.             _minute = dt.Minute;  //获得当前时间分钟

    489.             if (_minute != 0) _hour += 1;
    490.             offset = _hour / 2;
    491.             if (offset >= 12) offset = 0;
    492.             //zhiHour = zhiStr[offset].ToString();

    493.             //计算天干
    494.             TimeSpan ts = this._date - GanZhiStartDay;
    495.             i = ts.Days % 60;

    496.             indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1; //ganStr[i % 10] 为日的天干,(n*2-1) %10得出地支对应,n从1开始
    497.             tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(0, indexGan + 2);//凑齐12位
    498.             //ganHour = ganStr[((i % 10 + 1) * 2 - 1) % 10 - 1].ToString();

    499.             return tmpGan[offset].ToString() + zhiStr[offset].ToString();

    500.         }
    501.         #endregion

    502.         #region CheckDateLimit
    503.         /// <summary>
    504.         /// 检查公历日期是否符合要求
    505.         /// </summary>
    506.         /// <param name="dt"></param>
    507.         private void CheckDateLimit(DateTime dt)
    508.         {
    509.             if ((dt < MinDay) || (dt > MaxDay))
    510.             {
    511.                 throw new ChineseCalendarException("超出可转换的日期");
    512.             }
    513.         }
    514.         #endregion

    515.         #region CheckChineseDateLimit
    516.         /// <summary>
    517.         /// 检查农历日期是否合理
    518.         /// </summary>
    519.         /// <param name="year"></param>
    520.         /// <param name="month"></param>
    521.         /// <param name="day"></param>
    522.         /// <param name="leapMonth"></param>
    523.         private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
    524.         {
    525.             if ((year < MinYear) || (year > MaxYear))
    526.             {
    527.                 throw new ChineseCalendarException("非法农历日期");
    528.             }
    529.             if ((month < 1) || (month > 12))
    530.             {
    531.                 throw new ChineseCalendarException("非法农历日期");
    532.             }
    533.             if ((day < 1) || (day > 30)) //中国的月最多30天
    534.             {
    535.                 throw new ChineseCalendarException("非法农历日期");
    536.             }

    537.             int leap = GetChineseLeapMonth(year);// 计算该年应该闰哪个月
    538.             if ((leapMonth == true) && (month != leap))
    539.             {
    540.                 throw new ChineseCalendarException("非法农历日期");
    541.             }


    542.         }
    543.         #endregion

    544.         #region ConvertNumToChineseNum
    545.         /// <summary>
    546.         /// 将0-9转成汉字形式
    547.         /// </summary>
    548.         /// <param name="n"></param>
    549.         /// <returns></returns>
    550.         private string ConvertNumToChineseNum(char n)
    551.         {
    552.             if ((n < '0') || (n > '9')) return "";
    553.             switch (n)
    554.             {
    555.                 case '0':
    556.                     return HZNum[0].ToString();
    557.                 case '1':
    558.                     return HZNum[1].ToString();
    559.                 case '2':
    560.                     return HZNum[2].ToString();
    561.                 case '3':
    562.                     return HZNum[3].ToString();
    563.                 case '4':
    564.                     return HZNum[4].ToString();
    565.                 case '5':
    566.                     return HZNum[5].ToString();
    567.                 case '6':
    568.                     return HZNum[6].ToString();
    569.                 case '7':
    570.                     return HZNum[7].ToString();
    571.                 case '8':
    572.                     return HZNum[8].ToString();
    573.                 case '9':
    574.                     return HZNum[9].ToString();
    575.                 default:
    576.                     return "";
    577.             }
    578.         }
    579.         #endregion

    580.         #region BitTest32
    581.         /// <summary>
    582.         /// 测试某位是否为真
    583.         /// </summary>
    584.         /// <param name="num"></param>
    585.         /// <param name="bitpostion"></param>
    586.         /// <returns></returns>
    587.         private bool BitTest32(int num, int bitpostion)
    588.         {

    589.             if ((bitpostion > 31) || (bitpostion < 0))
    590.                 throw new Exception("Error Param: bitpostion[0-31]:" + bitpostion.ToString());

    591.             int bit = 1 << bitpostion;

    592.             if ((num & bit) == 0)
    593.             {
    594.                 return false;
    595.             }
    596.             else
    597.             {
    598.                 return true;
    599.             }
    600.         }
    601.         #endregion

    602.         #region ConvertDayOfWeek
    603.         /// <summary>
    604.         /// 将星期几转成数字表示
    605.         /// </summary>
    606.         /// <param name="dayOfWeek"></param>
    607.         /// <returns></returns>
    608.         private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
    609.         {
    610.             switch (dayOfWeek)
    611.             {
    612.                 case DayOfWeek.Sunday:
    613.                     return 1;
    614.                 case DayOfWeek.Monday:
    615.                     return 2;
    616.                 case DayOfWeek.Tuesday:
    617.                     return 3;
    618.                 case DayOfWeek.Wednesday:
    619.                     return 4;
    620.                 case DayOfWeek.Thursday:
    621.                     return 5;
    622.                 case DayOfWeek.Friday:
    623.                     return 6;
    624.                 case DayOfWeek.Saturday:
    625.                     return 7;
    626.                 default:
    627.                     return 0;
    628.             }
    629.         }
    630.         #endregion

    631.         #region CompareWeekDayHoliday
    632.         /// <summary>
    633.         /// 比较当天是不是指定的第周几
    634.         /// </summary>
    635.         /// <param name="date"></param>
    636.         /// <param name="month"></param>
    637.         /// <param name="week"></param>
    638.         /// <param name="day"></param>
    639.         /// <returns></returns>
    640.         private bool CompareWeekDayHoliday(DateTime date, int month, int week, int day)
    641.         {
    642.             bool ret = false;

    643.             if (date.Month == month) //月份相同
    644.             {
    645.                 if (ConvertDayOfWeek(date.DayOfWeek) == day) //星期几相同
    646.                 {
    647.                     DateTime firstDay = new DateTime(date.Year, date.Month, 1);//生成当月第一天
    648.                     int i = ConvertDayOfWeek(firstDay.DayOfWeek);
    649.                     int firWeekDays = 7 - ConvertDayOfWeek(firstDay.DayOfWeek) + 1; //计算第一周剩余天数

    650.                     if (i > day)
    651.                     {
    652.                         if ((week - 1) * 7 + day + firWeekDays == date.Day)
    653.                         {
    654.                             ret = true;
    655.                         }
    656.                     }
    657.                     else
    658.                     {
    659.                         if (day + firWeekDays + (week - 2) * 7 == date.Day)
    660.                         {
    661.                             ret = true;
    662.                         }
    663.                     }
    664.                 }
    665.             }

    666.             return ret;
    667.         }
    668.         #endregion
    669.         #endregion

    670.         #region  属性

    671.         #region 节日
    672.         #region ChineseCalendarHoliday
    673.         /// <summary>
    674.         /// 计算中国农历节日
    675.         /// </summary>
    676.         public string ChineseCalendarHoliday
    677.         {
    678.             get
    679.             {
    680.                 string tempStr = "";
    681.                 if (this._cIsLeapMonth == false) //闰月不计算节日
    682.                 {
    683.                     foreach (LunarHolidayStruct lh in lHolidayInfo)
    684.                     {
    685.                         if ((lh.Month == this._cMonth) && (lh.Day == this._cDay))
    686.                         {

    687.                             tempStr = lh.HolidayName;
    688.                             break;

    689.                         }
    690.                     }

    691.                     //对除夕进行特别处理
    692.                     if (this._cMonth == 12)
    693.                     {
    694.                         int i = GetChineseMonthDays(this._cYear, 12); //计算当年农历12月的总天数
    695.                         if (this._cDay == i) //如果为最后一天
    696.                         {
    697.                             tempStr = "除夕";
    698.                         }
    699.                     }
    700.                 }
    701.                 return tempStr;
    702.             }
    703.         }
    704.         #endregion

    705.         #region WeekDayHoliday
    706.         /// <summary>
    707.         /// 按某月第几周第几日计算的节日
    708.         /// </summary>
    709.         public string WeekDayHoliday
    710.         {
    711.             get
    712.             {
    713.                 string tempStr = "";
    714.                 foreach (WeekHolidayStruct wh in wHolidayInfo)
    715.                 {
    716.                     if (CompareWeekDayHoliday(_date, wh.Month, wh.WeekAtMonth, wh.WeekDay))
    717.                     {
    718.                         tempStr = wh.HolidayName;
    719.                         break;
    720.                     }
    721.                 }
    722.                 return tempStr;
    723.             }
    724.         }
    725.         #endregion

    726.         #region DateHoliday
    727.         /// <summary>
    728.         /// 按公历日计算的节日
    729.         /// </summary>
    730.         public string DateHoliday
    731.         {
    732.             get
    733.             {
    734.                 string tempStr = "";

    735.                 foreach (SolarHolidayStruct sh in sHolidayInfo)
    736.                 {
    737.                     if ((sh.Month == _date.Month) && (sh.Day == _date.Day))
    738.                     {
    739.                         tempStr = sh.HolidayName;
    740.                         break;
    741.                     }
    742.                 }
    743.                 return tempStr;
    744.             }
    745.         }
    746.         #endregion
    747.         #endregion

    748.         #region 公历日期
    749.         #region Date
    750.         /// <summary>
    751.         /// 取对应的公历日期
    752.         /// </summary>
    753.         public DateTime Date
    754.         {
    755.             get { return _date; }
    756.             set { _date = value; }
    757.         }
    758.         #endregion

    759.         #region WeekDay
    760.         /// <summary>
    761.         /// 取星期几
    762.         /// </summary>
    763.         public DayOfWeek WeekDay
    764.         {
    765.             get { return _date.DayOfWeek; }
    766.         }
    767.         #endregion

    768.         #region WeekDayStr
    769.         /// <summary>
    770.         /// 周几的字符
    771.         /// </summary>
    772.         public string WeekDayStr
    773.         {
    774.             get
    775.             {
    776.                 switch (_date.DayOfWeek)
    777.                 {
    778.                     case DayOfWeek.Sunday:
    779.                         return "星期日";
    780.                     case DayOfWeek.Monday:
    781.                         return "星期一";
    782.                     case DayOfWeek.Tuesday:
    783.                         return "星期二";
    784.                     case DayOfWeek.Wednesday:
    785.                         return "星期三";
    786.                     case DayOfWeek.Thursday:
    787.                         return "星期四";
    788.                     case DayOfWeek.Friday:
    789.                         return "星期五";
    790.                     default:
    791.                         return "星期六";
    792.                 }
    793.             }
    794.         }
    795.         #endregion

    796.         #region DateString
    797.         /// <summary>
    798.         /// 公历日期中文表示法 如一九九七年七月一日
    799.         /// </summary>
    800.         public string DateString
    801.         {
    802.             get
    803.             {
    804.                 return "公元" + this._date.ToLongDateString();
    805.             }
    806.         }
    807.         #endregion

    808.         #region IsLeapYear
    809.         /// <summary>
    810.         /// 当前是否公历闰年
    811.         /// </summary>
    812.         public bool IsLeapYear
    813.         {
    814.             get
    815.             {
    816.                 return DateTime.IsLeapYear(this._date.Year);
    817.             }
    818.         }
    819.         #endregion

    820.         #region ChineseConstellation
    821.         /// <summary>
    822.         /// 28星宿计算
    823.         /// </summary>
    824.         public string ChineseConstellation
    825.         {
    826.             get
    827.             {
    828.                 int offset = 0;
    829.                 int modStarDay = 0;

    830.                 TimeSpan ts = this._date - ChineseConstellationReferDay;
    831.                 offset = ts.Days;
    832.                 modStarDay = offset % 28;
    833.                 return (modStarDay >= 0 ? _chineseConstellationName[modStarDay] : _chineseConstellationName[27 + modStarDay]);
    834.             }
    835.         }
    836.         #endregion

    837.         #region ChineseHour
    838.         /// <summary>
    839.         /// 时辰
    840.         /// </summary>
    841.         public string ChineseHour
    842.         {
    843.             get
    844.             {
    845.                 return GetChineseHour(_datetime);
    846.             }
    847.         }
    848.         #endregion

    849.         #endregion

    850.         #region 农历日期
    851.         #region IsChineseLeapMonth
    852.         /// <summary>
    853.         /// 是否闰月
    854.         /// </summary>
    855.         public bool IsChineseLeapMonth
    856.         {
    857.             get { return this._cIsLeapMonth; }
    858.         }
    859.         #endregion

    860.         #region IsChineseLeapYear
    861.         /// <summary>
    862.         /// 当年是否有闰月
    863.         /// </summary>
    864.         public bool IsChineseLeapYear
    865.         {
    866.             get
    867.             {
    868.                 return this._cIsLeapYear;
    869.             }
    870.         }
    871.         #endregion

    872.         #region ChineseDay
    873.         /// <summary>
    874.         /// 农历日
    875.         /// </summary>
    876.         public int ChineseDay
    877.         {
    878.             get { return this._cDay; }
    879.         }
    880.         #endregion

    881.         #region ChineseDayString
    882.         /// <summary>
    883.         /// 农历日中文表示
    884.         /// </summary>
    885.         public string ChineseDayString
    886.         {
    887.             get
    888.             {
    889.                 switch (this._cDay)
    890.                 {
    891.                     case 0:
    892.                         return "";
    893.                     case 10:
    894.                         return "初十";
    895.                     case 20:
    896.                         return "二十";
    897.                     case 30:
    898.                         return "三十";
    899.                     default:
    900.                         return nStr2[(int)(_cDay / 10)].ToString() + nStr1[_cDay % 10].ToString();

    901.                 }
    902.             }
    903.         }
    904.         #endregion

    905.         #region ChineseMonth
    906.         /// <summary>
    907.         /// 农历的月份
    908.         /// </summary>
    909.         public int ChineseMonth
    910.         {
    911.             get { return this._cMonth; }
    912.         }
    913.         #endregion

    914.         #region ChineseMonthString
    915.         /// <summary>
    916.         /// 农历月份字符串
    917.         /// </summary>
    918.         public string ChineseMonthString
    919.         {
    920.             get
    921.             {
    922.                 return _monthString[this._cMonth];
    923.             }
    924.         }
    925.         #endregion

    926.         #region ChineseYear
    927.         /// <summary>
    928.         /// 取农历年份
    929.         /// </summary>
    930.         public int ChineseYear
    931.         {
    932.             get { return this._cYear; }
    933.         }
    934.         #endregion

    935.         #region ChineseYearString
    936.         /// <summary>
    937.         /// 取农历年字符串如,一九九七年
    938.         /// </summary>
    939.         public string ChineseYearString
    940.         {
    941.             get
    942.             {
    943.                 string tempStr = "";
    944.                 string num = this._cYear.ToString();
    945.                 for (int i = 0; i < 4; i++)
    946.                 {
    947.                     tempStr += ConvertNumToChineseNum(num[i]);
    948.                 }
    949.                 return tempStr + "年";
    950.             }
    951.         }
    952.         #endregion

    953.         #region ChineseDateString
    954.         /// <summary>
    955.         /// 取农历日期表示法:农历一九九七年正月初五
    956.         /// </summary>
    957.         public string ChineseDateString
    958.         {
    959.             get
    960.             {
    961.                 if (this._cIsLeapMonth == true)
    962.                 {
    963.                     return "农历" + ChineseYearString + "闰" + ChineseMonthString + ChineseDayString;
    964.                 }
    965.                 else
    966.                 {
    967.                     return "农历" + ChineseYearString + ChineseMonthString + ChineseDayString;
    968.                 }
    969.             }
    970.         }
    971.         #endregion

    972.         #region ChineseTwentyFourDay
    973.         /// <summary>
    974.         /// 定气法计算二十四节气,二十四节气是按地球公转来计算的,并非是阴历计算的
    975.         /// </summary>
    976.         /// <remarks>
    977.         /// 节气的定法有两种。古代历法采用的称为"恒气",即按时间把一年等分为24份,
    978.         /// 每一节气平均得15天有余,所以又称"平气"。现代农历采用的称为"定气",即
    979.         /// 按地球在轨道上的位置为标准,一周360°,两节气之间相隔15°。由于冬至时地
    980.         /// 球位于近日点附近,运动速度较快,因而太阳在黄道上移动15°的时间不到15天。
    981.         /// 夏至前后的情况正好相反,太阳在黄道上移动较慢,一个节气达16天之多。采用
    982.         /// 定气时可以保证春、秋两分必然在昼夜平分的那两天。
    983.         /// </remarks>
    984.         public string ChineseTwentyFourDay
    985.         {
    986.             get
    987.             {
    988.                 DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
    989.                 DateTime newDate;
    990.                 double num;
    991.                 int y;
    992.                 string tempStr = "";

    993.                 y = this._date.Year;

    994.                 for (int i = 1; i <= 24; i++)
    995.                 {
    996.                     num = 525948.76 * (y - 1900) + sTermInfo[i - 1];

    997.                     newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
    998.                     if (newDate.DayOfYear == _date.DayOfYear)
    999.                     {
    1000.                         tempStr = SolarTerm[i - 1];
    1001.                         break;
    1002.                     }
    1003.                 }
    1004.                 return tempStr;
    1005.             }
    1006.         }

    1007.         //当前日期前一个最近节气
    1008.         public string ChineseTwentyFourPrevDay
    1009.         {
    1010.             get
    1011.             {
    1012.                 DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
    1013.                 DateTime newDate;
    1014.                 double num;
    1015.                 int y;
    1016.                 string tempStr = "";

    1017.                 y = this._date.Year;

    1018.                 for (int i = 24; i >= 1; i--)
    1019.                 {
    1020.                     num = 525948.76 * (y - 1900) + sTermInfo[i - 1];

    1021.                     newDate = baseDateAndTime.AddMinutes(num);//按分钟计算

    1022.                     if (newDate.DayOfYear < _date.DayOfYear)
    1023.                     {
    1024.                         tempStr = string.Format("{0}[{1}]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"));
    1025.                         break;
    1026.                     }
    1027.                 }

    1028.                 return tempStr;
    1029.             }

    1030.         }

    1031.         //当前日期后一个最近节气
    1032.         public string ChineseTwentyFourNextDay
    1033.         {
    1034.             get
    1035.             {
    1036.                 DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
    1037.                 DateTime newDate;
    1038.                 double num;
    1039.                 int y;
    1040.                 string tempStr = "";

    1041.                 y = this._date.Year;

    1042.                 for (int i = 1; i <= 24; i++)
    1043.                 {
    1044.                     num = 525948.76 * (y - 1900) + sTermInfo[i - 1];

    1045.                     newDate = baseDateAndTime.AddMinutes(num);//按分钟计算

    1046.                     if (newDate.DayOfYear > _date.DayOfYear)
    1047.                     {
    1048.                         tempStr = string.Format("{0}[{1}]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"));
    1049.                         break;
    1050.                     }
    1051.                 }
    1052.                 return tempStr;
    1053.             }

    1054.         }
    1055.         #endregion
    1056.         #endregion

    1057.         #region 星座
    1058.         #region Constellation
    1059.         /// <summary>
    1060.         /// 计算指定日期的星座序号
    1061.         /// </summary>
    1062.         /// <returns></returns>
    1063.         public string Constellation
    1064.         {
    1065.             get
    1066.             {
    1067.                 int index = 0;
    1068.                 int y, m, d;
    1069.                 y = _date.Year;
    1070.                 m = _date.Month;
    1071.                 d = _date.Day;
    1072.                 y = m * 100 + d;

    1073.                 if (((y >= 321) && (y <= 419))) { index = 0; }
    1074.                 else if ((y >= 420) && (y <= 520)) { index = 1; }
    1075.                 else if ((y >= 521) && (y <= 620)) { index = 2; }
    1076.                 else if ((y >= 621) && (y <= 722)) { index = 3; }
    1077.                 else if ((y >= 723) && (y <= 822)) { index = 4; }
    1078.                 else if ((y >= 823) && (y <= 922)) { index = 5; }
    1079.                 else if ((y >= 923) && (y <= 1022)) { index = 6; }
    1080.                 else if ((y >= 1023) && (y <= 1121)) { index = 7; }
    1081.                 else if ((y >= 1122) && (y <= 1221)) { index = 8; }
    1082.                 else if ((y >= 1222) || (y <= 119)) { index = 9; }
    1083.                 else if ((y >= 120) && (y <= 218)) { index = 10; }
    1084.                 else if ((y >= 219) && (y <= 320)) { index = 11; }
    1085.                 else { index = 0; }

    1086.                 return _constellationName[index];
    1087.             }
    1088.         }
    1089.         #endregion
    1090.         #endregion

    1091.         #region 属相
    1092.         #region Animal
    1093.         /// <summary>
    1094.         /// 计算属相的索引,注意虽然属相是以农历年来区别的,但是目前在实际使用中是按公历来计算的
    1095.         /// 鼠年为1,其它类推
    1096.         /// </summary>
    1097.         public int Animal
    1098.         {
    1099.             get
    1100.             {
    1101.                 int offset = _date.Year - AnimalStartYear;
    1102.                 return (offset % 12) + 1;
    1103.             }
    1104.         }
    1105.         #endregion

    1106.         #region AnimalString
    1107.         /// <summary>
    1108.         /// 取属相字符串
    1109.         /// </summary>
    1110.         public string AnimalString
    1111.         {
    1112.             get
    1113.             {
    1114.                 int offset = _date.Year - AnimalStartYear; //阳历计算
    1115.                 //int offset = this._cYear - AnimalStartYear; 农历计算
    1116.                 return animalStr[offset % 12].ToString();
    1117.             }
    1118.         }
    1119.         #endregion
    1120.         #endregion

    1121.         #region 天干地支
    1122.         #region GanZhiYearString
    1123.         /// <summary>
    1124.         /// 取农历年的干支表示法如 乙丑年
    1125.         /// </summary>
    1126.         public string GanZhiYearString
    1127.         {
    1128.             get
    1129.             {
    1130.                 string tempStr;
    1131.                 int i = (this._cYear - GanZhiStartYear) % 60; //计算干支
    1132.                 tempStr = ganStr[i % 10].ToString() + zhiStr[i % 12].ToString() + "年";
    1133.                 return tempStr;
    1134.             }
    1135.         }
    1136.         #endregion

    1137.         #region GanZhiMonthString
    1138.         /// <summary>
    1139.         /// 取干支的月表示字符串,注意农历的闰月不记干支
    1140.         /// </summary>
    1141.         public string GanZhiMonthString
    1142.         {
    1143.             get
    1144.             {
    1145.                 //每个月的地支总是固定的,而且总是从寅月开始
    1146.                 int zhiIndex;
    1147.                 string zhi;
    1148.                 if (this._cMonth > 10)
    1149.                 {
    1150.                     zhiIndex = this._cMonth - 10;
    1151.                 }
    1152.                 else
    1153.                 {
    1154.                     zhiIndex = this._cMonth + 2;
    1155.                 }
    1156.                 zhi = zhiStr[zhiIndex - 1].ToString();

    1157.                 //根据当年的干支年的干来计算月干的第一个
    1158.                 int ganIndex = 1;
    1159.                 string gan;
    1160.                 int i = (this._cYear - GanZhiStartYear) % 60; //计算干支
    1161.                 switch (i % 10)
    1162.                 {
    1163.                     #region ...
    1164.                     case 0: //甲
    1165.                         ganIndex = 3;
    1166.                         break;
    1167.                     case 1: //乙
    1168.                         ganIndex = 5;
    1169.                         break;
    1170.                     case 2: //丙
    1171.                         ganIndex = 7;
    1172.                         break;
    1173.                     case 3: //丁
    1174.                         ganIndex = 9;
    1175.                         break;
    1176.                     case 4: //戊
    1177.                         ganIndex = 1;
    1178.                         break;
    1179.                     case 5: //己
    1180.                         ganIndex = 3;
    1181.                         break;
    1182.                     case 6: //庚
    1183.                         ganIndex = 5;
    1184.                         break;
    1185.                     case 7: //辛
    1186.                         ganIndex = 7;
    1187.                         break;
    1188.                     case 8: //壬
    1189.                         ganIndex = 9;
    1190.                         break;
    1191.                     case 9: //癸
    1192.                         ganIndex = 1;
    1193.                         break;
    1194.                     #endregion
    1195.                 }
    1196.                 gan = ganStr[(ganIndex + this._cMonth - 2) % 10].ToString();

    1197.                 return gan + zhi + "月";
    1198.             }
    1199.         }
    1200.         #endregion

    1201.         #region GanZhiDayString
    1202.         /// <summary>
    1203.         /// 取干支日表示法
    1204.         /// </summary>
    1205.         public string GanZhiDayString
    1206.         {
    1207.             get
    1208.             {
    1209.                 int i, offset;
    1210.                 TimeSpan ts = this._date - GanZhiStartDay;
    1211.                 offset = ts.Days;
    1212.                 i = offset % 60;
    1213.                 return ganStr[i % 10].ToString() + zhiStr[i % 12].ToString() + "日";
    1214.             }
    1215.         }
    1216.         #endregion

    1217.         #region GanZhiDateString
    1218.         /// <summary>
    1219.         /// 取当前日期的干支表示法如 甲子年乙丑月丙庚日
    1220.         /// </summary>
    1221.         public string GanZhiDateString
    1222.         {
    1223.             get
    1224.             {
    1225.                 return GanZhiYearString + GanZhiMonthString + GanZhiDayString;
    1226.             }
    1227.         }
    1228.         #endregion
    1229.         #endregion
    1230.         #endregion

    1231.         #region 方法
    1232.         #region NextDay
    1233.         /// <summary>
    1234.         /// 取下一天
    1235.         /// </summary>
    1236.         /// <returns></returns>
    1237.         public ChineseCalendar NextDay()
    1238.         {
    1239.             DateTime nextDay = _date.AddDays(1);
    1240.             return new ChineseCalendar(nextDay);
    1241.         }
    1242.         #endregion

    1243.         #region PervDay
    1244.         /// <summary>
    1245.         /// 取前一天
    1246.         /// </summary>
    1247.         /// <returns></returns>
    1248.         public ChineseCalendar PervDay()
    1249.         {
    1250.             DateTime pervDay = _date.AddDays(-1);
    1251.             return new ChineseCalendar(pervDay);
    1252.         }
    1253.         #endregion
    1254.         #endregion
    1255.     }
    1256. }
    复制代码


    调用:
    1. DateTime dt = DateTime.Now;
    2. ChineseCalendar cc = new ChineseCalendar(dt);
    3. Console.WriteLine("阳历:" + cc.DateString);
    4. Console.WriteLine("属相:" + cc.AnimalString);
    5. Console.WriteLine("农历:" + cc.ChineseDateString);
    6. Console.WriteLine("时辰:" + cc.ChineseHour);
    7. Console.WriteLine("节气:" + cc.ChineseTwentyFourDay);
    8. Console.WriteLine("节日:" + cc.DateHoliday);
    9. Console.WriteLine("前一个节气:" + cc.ChineseTwentyFourPrevDay);
    10. Console.WriteLine("后一个节气:" + cc.ChineseTwentyFourNextDay);
    11. Console.WriteLine("干支:" + cc.GanZhiDateString);
    12. Console.WriteLine("星期:" + cc.WeekDayStr);
    13. Console.WriteLine("星宿:" + cc.ChineseConstellation);
    14. Console.WriteLine("星座:" + cc.Constellation);
    复制代码



    结果:
    1. 阳历:公元2013年1月27日
    2. 属相:蛇
    3. 农历:农历二零一二年腊月十六
    4. 时辰:庚申
    5. 节气:
    6. 节日:
    7. 前一个节气:大寒[2013-01-20]
    8. 后一个节气:立春[2013-02-03]
    9. 干支:壬辰年癸丑月癸巳日
    10. 星期:星期日
    11. 星宿:房日兔
    12. 星座:水瓶座
    复制代码

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|小黑屋|Silian Lighting+ ( 蜀ICP备14004521号-1 )

    GMT+8, 2024-5-6 18:52 , Processed in 1.093750 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表