/// <summary> ///參考互網資源 /// 中国萬年曆靜態查詢節日操作基類 /// 20120908 修改 塗聚文 /// 20121010 修改 塗聚文 /// 締友計算機信息技術有限公司 /// 捷為工作室 /// </summary> public static class ChineseDate { /// <summary> /// 中國農曆 /// </summary> private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar(); /// <summary> /// 公曆節日 /// </summary> private static Hashtable gHoliday = new Hashtable(); /// <summary> /// 農曆節日 /// </summary> private static Hashtable nHoliday = new Hashtable(); /// <summary> /// 月中周節日 /// </summary> private static Hashtable weekHolidays = new Hashtable(); /// <summary> /// 年中第幾日節日 /// </summary> private static Hashtable inYearDayHolidays = new Hashtable(); /// <summary> /// 指定日期節日 /// </summary> private static Hashtable specifiedHolidays = new Hashtable(); /// <summary> /// 結婚紀念日 /// </summary> private static Hashtable weddingHolidays = new Hashtable(); /// <summary> /// 結婚紀念日名稱 /// </summary> private static Hashtable weddingNameList = new Hashtable(); private static int currentWeek = 0;//记录周数 /// <summary> /// /// </summary> private static string[] JQ = { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑" , "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" }; /// <summary> /// /// </summary> private static int[] JQData = { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 }; /// <summary> /// /// </summary> static ChineseDate() { //公历节日 gHoliday.Add("0101", "元旦"); gHoliday.Add("0114", "日记情人节(Diary Day) ");//Diary Day 日记情人节 gHoliday.Add("0202", "世界湿地日 "); gHoliday.Add("0210", "国际气象节 "); gHoliday.Add("0214", "圣瓦伦丁节(Valentine’s Day ),塗聚文 ");//Valentine’s Day 传统情人节 gHoliday.Add("0301", "国际海豹日 "); gHoliday.Add("0303", "全国爱耳日 "); gHoliday.Add("0305", "雷锋日 "); gHoliday.Add("0308", "妇女节 "); gHoliday.Add("0312", "植树节 "); gHoliday.Add("0314", "白色情人节(White Day ),国际警察日 ");//White Day 白色情人节 gHoliday.Add("0315", "消费者权益日 "); gHoliday.Add("0317", "中国国医节 国际航海日 "); gHoliday.Add("0321", "世界森林日 消除种族歧视国际日 世界儿歌日 "); gHoliday.Add("0322", "世界水日 "); gHoliday.Add("0323", "世界气象日 "); gHoliday.Add("0324", "世界防治结核病日 "); gHoliday.Add("0325", "全国中小学生安全教育日 "); gHoliday.Add("0330", "巴勒斯坦国土日 "); gHoliday.Add("0401", "愚人节 全国爱国卫生运动月(四月) 税收宣传月(四月) "); gHoliday.Add("0414", "黑色情人节(Black Day) ");//Black Day 黑色情人节 gHoliday.Add("0407", "世界卫生日 "); gHoliday.Add("0422", "世界地球日 "); gHoliday.Add("0423", "世界图书和版权日 "); gHoliday.Add("0424", "亚非新闻工作者日 "); gHoliday.Add("0501", "劳动节 "); gHoliday.Add("0504", "青年节 "); gHoliday.Add("0505", "碘缺乏病防治日 "); gHoliday.Add("0508", "世界红十字日 "); gHoliday.Add("0512", "国际护士节 "); gHoliday.Add("0514", "玫瑰情人节(Yellow &Rose Day) ");//Yellow &Rose Day 玫瑰情人节 gHoliday.Add("0515", "国际家庭日 "); gHoliday.Add("0517", "国际电信日 "); gHoliday.Add("0518", "国际博物馆日 "); gHoliday.Add("0519", "中国旅游日 "); gHoliday.Add("0520", "全国学生营养日 中国旅游日 "); gHoliday.Add("0523", "国际牛奶日 "); gHoliday.Add("0531", "世界无烟日 "); gHoliday.Add("0601", "国际儿童节 "); gHoliday.Add("0605", "世界环境保护日 "); gHoliday.Add("0606", "全国爱眼日 "); gHoliday.Add("0614", "亲亲情人节(Kiss Day) ");//Kiss Day 亲亲情人节 gHoliday.Add("0617", "防治荒漠化和干旱日 "); gHoliday.Add("0623", "国际奥林匹克日 "); gHoliday.Add("0625", "全国土地日 "); gHoliday.Add("0626", "国际禁毒日 "); gHoliday.Add("0701", "建党节 香港回归纪念日 中共诞辰 世界建筑日 "); gHoliday.Add("0702", "国际体育记者日 "); gHoliday.Add("0707", "抗日战争纪念日 "); gHoliday.Add("0711", "世界人口日 "); gHoliday.Add("0714", "绿色情人节(Green Day) ");//Green Day 绿色情人节 gHoliday.Add("0730", "非洲妇女日 "); gHoliday.Add("0801", "建军节 "); gHoliday.Add("0808", "中国男子节(爸爸节) "); gHoliday.Add("0814", "银色情人节(Silver Day) ");//Silver Day 银色情人节 gHoliday.Add("0815", "抗日战争胜利纪念 "); gHoliday.Add("0908", "国际扫盲日 国际新闻工作者日 "); gHoliday.Add("0909", "毛主席逝世纪念 "); gHoliday.Add("0910", "教师节 "); gHoliday.Add("0914", "相片情人节(Music &Photo Day) 世界清洁地球日 ");//Music &Photo Day 相片情人节 gHoliday.Add("0916", "国际臭氧层保护日 "); gHoliday.Add("0918", "九·一八事变纪念日 "); gHoliday.Add("0920", "国际爱牙日 "); gHoliday.Add("0927", "世界旅游日 "); gHoliday.Add("0928", "孔子诞辰 "); gHoliday.Add("1001", "国庆节 世界音乐日 国际老人节 "); gHoliday.Add("1004", "世界动物日 "); gHoliday.Add("1006", "老人节 "); gHoliday.Add("1008", "全国高血压日 世界视觉日 "); gHoliday.Add("1009", "世界邮政日 万国邮联日 "); gHoliday.Add("1010", "辛亥革命纪念日 世界精神卫生日 "); gHoliday.Add("1013", "世界保健日 国际教师节 "); gHoliday.Add("1014", "葡萄酒情人节(Wine Day) 世界标准日 ");//Wine Day 葡萄酒情人节 gHoliday.Add("1015", "国际盲人节(白手杖节) "); gHoliday.Add("1016", "世界粮食日 "); gHoliday.Add("1017", "世界消除贫困日 "); gHoliday.Add("1022", "世界传统医药日 "); gHoliday.Add("1024", "联合国日 "); gHoliday.Add("1031", "世界勤俭日 "); gHoliday.Add("1107", "十月社会主义革命纪念日 "); gHoliday.Add("1108", "中国记者日 "); gHoliday.Add("1109", "全国消防安全宣传教育日 "); gHoliday.Add("1110", "世界青年节 "); gHoliday.Add("1111", "国际科学与和平周(本日所属的一周) "); gHoliday.Add("1112", "孙中山诞辰纪念日 "); gHoliday.Add("1114", "电影情人节(Orange & Movie Day) 世界糖尿病日 ");//Orange & Movie Day 电影情人节 gHoliday.Add("1117", "国际大学生节 世界学生节 "); gHoliday.Add("1120", "彝族年 "); gHoliday.Add("1121", "彝族年 世界问候日 世界电视日 "); gHoliday.Add("1122", "彝族年 "); gHoliday.Add("1129", "国际声援巴勒斯坦人民国际日 "); gHoliday.Add("1201", "世界艾滋病日 "); gHoliday.Add("1203", "世界残疾人日 "); gHoliday.Add("1205", "国际经济和社会发展志愿人员日 "); gHoliday.Add("1208", "国际儿童电视日 "); gHoliday.Add("1209", "世界足球日 "); gHoliday.Add("1210", "世界人权日 "); gHoliday.Add("1212", "西安事变纪念日 "); gHoliday.Add("1213", "南京大屠杀(1937年)纪念日!紧记血泪史! "); gHoliday.Add("1214", "拥抱情人节(Hug Day) ");//Hug Day 拥抱情人节 gHoliday.Add("1220", "澳门回归纪念 "); gHoliday.Add("1221", "国际篮球日 "); gHoliday.Add("1224", "平安夜 "); gHoliday.Add("1225", "圣诞节 "); gHoliday.Add("1226", "毛主席诞辰纪念 "); //农历节日(年份數據不正確,只做測試) nHoliday.Add("0101", "0028春节"); nHoliday.Add("0115", "0026元宵节"); nHoliday.Add("0202", "0027龙抬头节"); nHoliday.Add("0505", "0021端午节"); nHoliday.Add("0707", "0820七夕(乞巧节)");// nHoliday.Add("0715", "0020中元节(鬼节)");// nHoliday.Add("0815", "0023中秋节"); nHoliday.Add("0909", "0026重阳节"); nHoliday.Add("1001", "0021寒衣节"); nHoliday.Add("1208", "0028腊八节"); nHoliday.Add("1223", "0120灶君(祭灶)节");// nHoliday.Add("1224", "0220小年"); nHoliday.Add("1227", "1976塗聚文"); //周節日 weekHolidays.Add("050201", "1907母亲节Mother's Day"); weekHolidays.Add("050301", "1990全国助残日"); weekHolidays.Add("060301", "1910父亲节"); weekHolidays.Add("090303", "1981国际和平日"); weekHolidays.Add("090401", "1958国际聋人节"); weekHolidays.Add("100102", "1985国际住房日"); weekHolidays.Add("100104", "1987国际减轻自然灾害日"); weekHolidays.Add("110405", "1941感恩节Thanksgiving Day"); //weekHolidays.Add("", ""); //年中第幾日節日 inYearDayHolidays.Add("256", "2002程序员节(День программиста)"); //指定節日 specifiedHolidays.Add("20000423", "復活節(西欧)"); specifiedHolidays.Add("20000430", "復活節(东正教)"); specifiedHolidays.Add("20010415", "復活節"); specifiedHolidays.Add("20020331", "復活節(西歐)"); specifiedHolidays.Add("20020505", "復活節(東正教)"); specifiedHolidays.Add("20030420", "復活節(西歐)"); specifiedHolidays.Add("20030427", "復活節(東正教)"); specifiedHolidays.Add("20040411", "復活節"); specifiedHolidays.Add("20050327", "復活節(西歐)"); specifiedHolidays.Add("20050501", "復活節(東正教)"); specifiedHolidays.Add("20060416", "復活節(西歐)"); specifiedHolidays.Add("20060423", "復活節(東正教)"); specifiedHolidays.Add("20070408", "復活節"); specifiedHolidays.Add("20080323", "復活節(西歐)"); specifiedHolidays.Add("20080427", "復活節(東正教)"); specifiedHolidays.Add("20090412", "復活節(西歐)"); specifiedHolidays.Add("20090419", "復活節(東正教)"); specifiedHolidays.Add("20100404", "復活節"); specifiedHolidays.Add("20110424", "復活節"); specifiedHolidays.Add("20120408", "復活節(西歐)"); specifiedHolidays.Add("20120415", "復活節(東正教)"); specifiedHolidays.Add("20130331", "復活節(西歐)"); specifiedHolidays.Add("20130505", "復活節(東正教)"); specifiedHolidays.Add("20140420", "復活節"); specifiedHolidays.Add("20150405", "復活節(西歐)"); specifiedHolidays.Add("20150412", "復活節(東正教)"); specifiedHolidays.Add("20160327", "復活節(西歐)"); specifiedHolidays.Add("20160501", "復活節(東正教)"); specifiedHolidays.Add("20170416", "復活節"); specifiedHolidays.Add("20180401", "復活節(西歐)"); specifiedHolidays.Add("20190421", "復活節(西歐)"); specifiedHolidays.Add("20190428", "復活節(東正教)"); specifiedHolidays.Add("20200412", "復活節(西歐)"); specifiedHolidays.Add("20200419", "復活節(東正教)"); //specifiedHolidays.Add("", ""); //specifiedHolidays.Add("", ""); // 結婚紀念日名稱 weddingHolidays.Add("0928", "2002劉軍與天金"); weddingHolidays.Add("0115", "2003阿文與阿紅"); weddingNameList.Add(1, "紙婚(Paper Wedding)"); weddingNameList.Add(2, "布婚/棉布婚(Calico Wedding/Cotton Wedding)"); weddingNameList.Add(3, "羊布婚/皮婚(Muslim Wedding/Leather Wedding)"); weddingNameList.Add(4, "絲婚(Silk Wedding)"); weddingNameList.Add(5, "木婚(Wood Wedding)"); weddingNameList.Add(6, "鐵婚(Iron Wedding)"); weddingNameList.Add(7, "銅婚(Copper Wedding)"); weddingNameList.Add(8, "陶器婚(Pottery Wedding)"); weddingNameList.Add(9, "柳婚(Willow Wedding)"); weddingNameList.Add(10, "錫婚(Tin Wedding)"); weddingNameList.Add(11, "鋼婚(Steel Wedding)"); weddingNameList.Add(12, "鏈婚(Chain Wedding)"); weddingNameList.Add(13, "花邊婚(Lace Wedding)"); weddingNameList.Add(14, "象牙婚(Ivory Wedding)"); weddingNameList.Add(15, "水晶婚(Crystal Wedding)"); weddingNameList.Add(20, "搪瓷婚(China Wedding)"); weddingNameList.Add(25, "銀婚(Silver Wedding/Silver Jubilee)"); weddingNameList.Add(30, "珍珠婚(Pearl Wedding)"); weddingNameList.Add(35, "珊瑚婚/碧玉婚(Coral Wedding/Jade Wedding)"); weddingNameList.Add(40, "紅寶石婚(Ruby Wedding)"); weddingNameList.Add(45, "藍寶石婚(Sapphire Wedding)"); weddingNameList.Add(50, "金婚(Golden Wedding)"); weddingNameList.Add(55, "翡翠婚(Emerald Wedding)"); weddingNameList.Add(60, "鑽石婚(Diamond Wedding/Diamond Jubilee)"); } /// <summary> /// 获取农历 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetChinaDate(DateTime dt) { if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime) { //日期范围:1901 年 2 月 19 日 - 2049 年 12 月 30 日 throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", china.MinSupportedDateTime.ToString("yyyy-MM-dd"), china.MaxSupportedDateTime.ToString("yyyy-MM-dd"))); } string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt)); string strJQ = GetSolarTerm(dt); if (strJQ != "") { str += " (" + strJQ + ")"; } string strHoliday = GetHoliday(dt); if (strHoliday != "") { str += " " + strHoliday; } string strChinaHoliday = GetChinaHoliday(dt); if (strChinaHoliday != "") { str += " " + strChinaHoliday; } return str; } /// <summary> /// 获取农历年份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetYear(DateTime dt) { int yearIndex = china.GetSexagenaryYear(dt); string yearTG = " 甲乙丙丁戊己庚辛壬癸"; string yearDZ = " 子丑寅卯辰巳午未申酉戌亥"; string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪"; int year = china.GetYear(dt); int yTG = china.GetCelestialStem(yearIndex); int yDZ = china.GetTerrestrialBranch(yearIndex); string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]); return str; } /// <summary> /// 获取农历月份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetMonth(DateTime dt) { int year = china.GetYear(dt); int iMonth = china.GetMonth(dt); int leapMonth = china.GetLeapMonth(year); bool isLeapMonth = iMonth == leapMonth; if (leapMonth != 0 && iMonth >= leapMonth) { iMonth--; } string szText = "正二三四五六七八九十"; string strMonth = isLeapMonth ? "闰" : ""; if (iMonth <= 10) { strMonth += szText.Substring(iMonth - 1, 1); } else if (iMonth == 11) { strMonth += "十一"; } else { strMonth += "腊"; } return strMonth + "月"; } /// <summary> /// 數字農曆月份 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static int GetMonthInt(DateTime dt) { int year = china.GetYear(dt); int iMonth = china.GetMonth(dt); //闰月 int leapMonth = china.GetLeapMonth(year); bool isLeapMonth = iMonth == leapMonth; if (leapMonth != 0 && iMonth >= leapMonth) { iMonth--; } return iMonth; } /// <summary> /// 获取农历日期 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetDay(DateTime dt) { int iDay = china.GetDayOfMonth(dt); string szText1 = "初十廿三"; string szText2 = "一二三四五六七八九十"; string strDay; if (iDay == 20) { strDay = "二十"; } else if (iDay == 30) { strDay = "三十"; } else { strDay = szText1.Substring((iDay - 1) / 10, 1); strDay = strDay + szText2.Substring((iDay - 1) % 10, 1); } return strDay; } /// <summary> /// 获取节气 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetSolarTerm(DateTime dt) { DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0); DateTime dtNew; double num; int y; string strReturn = ""; y = dt.Year; for (int i = 1; i <= 24; i++) { num = 525948.76 * (y - 1900) + JQData[i - 1]; dtNew = dtBase.AddMinutes(num); if (dtNew.DayOfYear == dt.DayOfYear) { strReturn = JQ[i - 1]; } } return strReturn; } /// <summary> /// 获取公历节日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetHoliday(DateTime dt) { //int y = dt.Year; string strReturn = ""; //int sy = 0; object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")]; if (g != null) { strReturn = g.ToString(); } return strReturn; } /// <summary> /// 获取农历节日 並獲取周年 /// 塗聚文 /// </summary> /// <param name="dt">公曆時間</param> /// <returns>返回節日名稱和周年</returns> public static string GetChinaHoliday(DateTime dt) { string strReturn = ""; int year = china.GetYear(dt); int iMonth = china.GetMonth(dt); int leapMonth = china.GetLeapMonth(year); int iDay = china.GetDayOfMonth(dt); int sy = 0; if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)) { strReturn = "除夕"; } else if (leapMonth != iMonth) { if (leapMonth != 0 && iMonth >= leapMonth) { iMonth--; } object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")]; if (n != null) { if (strReturn == "") { strReturn = n.ToString(); sy = year - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份,並以在此年份這後顯示節日 if (sy >= 0) { strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; } else { strReturn = ""; } } else { strReturn += " " + n.ToString(); } } } return strReturn; } /// <summary> /// 周期性節日 /// </summary> /// <param name="dt">公曆時間</param> /// <returns>返回節日名稱和周年</returns> public static string GetWeekHoliday(DateTime dt) { int y = dt.Year; string strReturn = ""; int sy = 0; int weeks = week(dt); //第幾周 int weekday = ConvertDayOfWeek(dt.DayOfWeek);//周幾 object g = weekHolidays[dt.Month.ToString("00") + weeks.ToString("00")+weekday.ToString("00")]; if (g != null) { strReturn = g.ToString(); sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 if (sy >= 0) //必須之後的年份才可以顯示 { strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; } else { strReturn = string.Empty; } } return strReturn; } /// <summary> ///年中第幾日節日 程序員節 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetInYearMonth(DateTime dt) { int y = dt.Year; int sy = 0; string strReturn = ""; int inyear = dt.DayOfYear; object g = inYearDayHolidays[inyear.ToString("000")]; if (g != null) { strReturn = g.ToString(); sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 if (sy >= 0) //必須之後的年份才可以顯示 { strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; } else { strReturn = string.Empty; } } return strReturn; } /// <summary> /// 指定的節日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetSpecifiedDay(DateTime dt) { string strReturn = ""; object g = specifiedHolidays[dt.Year.ToString("0000") + dt.Month.ToString("00") + dt.Day.ToString("00")]; if (g != null) { strReturn = g.ToString(); } return strReturn; } /// <summary> /// 結婚紀念日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetWeddingHoliday(DateTime dt) { int y = dt.Year; int sy = 0; string strReturn = ""; string nameRetur = string.Empty; object g = weddingHolidays[dt.Month.ToString("00") + dt.Day.ToString("00")]; if (g != null) { strReturn = g.ToString(); sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 if (sy >= 0) //必須之後的年份才可以顯示 { strReturn = strReturn.Substring(4, strReturn.Length - 4); object name = weddingNameList[sy]; if (name != null) { nameRetur = "〖" + name.ToString() + "紀念〗"; } else { nameRetur = "〖" + sy.ToString() + "周年結婚紀念〗"; } } else { strReturn = ""; } } return strReturn + nameRetur; } /// <summary> /// 将星期几转成数字表示 /// </summary> /// <param name="dayOfWeek"></param> /// <returns></returns> private static int ConvertDayOfWeek(DayOfWeek dayOfWeek) { switch (dayOfWeek) { case DayOfWeek.Sunday: return 1; case DayOfWeek.Monday: return 2; case DayOfWeek.Tuesday: return 3; case DayOfWeek.Wednesday: return 4; case DayOfWeek.Thursday: return 5; case DayOfWeek.Friday: return 6; case DayOfWeek.Saturday: return 7; default: return 0; } } /// <summary> /// 当前日期是本月的第几周 /// </summary> /// <param name="day"></param> /// <returns></returns> private static int week(DateTime day) { string y = day.Year.ToString(); string m = day.Month.ToString(); string s = y + "-" + m + "-1"; DateTime dt = DateTime.Parse(s); int d = day.Day - dt.Day; int w = 1; for (int i = 1; i <= d; i++) { DateTime dt1 = dt.AddDays(i); if (dt1.DayOfWeek == DayOfWeek.Sunday) { w = w + 1; } } return w; } }
时间: 2024-09-10 22:14:09