1 /// <summary> 2 ///參考互網資源 3 /// 中国萬年曆靜態查詢節日操作基類 4 /// 20120908 修改 塗聚文 5 /// 20121010 修改 塗聚文 6 /// 締友計算機信息技術有限公司 7 /// 捷為工作室 8 /// </summary> 9 public static class ChineseDate 10 { 11 /// <summary> 12 /// 中國農曆 13 /// </summary> 14 private static ChineseLunisolarCalendar china = new ChineseLunisolarCalendar(); 15 /// <summary> 16 /// 公曆節日 17 /// </summary> 18 private static Hashtable gHoliday = new Hashtable(); 19 /// <summary> 20 /// 農曆節日 21 /// </summary> 22 private static Hashtable nHoliday = new Hashtable(); 23 /// <summary> 24 /// 月中周節日 25 /// </summary> 26 private static Hashtable weekHolidays = new Hashtable(); 27 /// <summary> 28 /// 年中第幾日節日 29 /// </summary> 30 private static Hashtable inYearDayHolidays = new Hashtable(); 31 /// <summary> 32 /// 指定日期節日 33 /// </summary> 34 private static Hashtable specifiedHolidays = new Hashtable(); 35 36 /// <summary> 37 /// 結婚紀念日 38 /// </summary> 39 private static Hashtable weddingHolidays = new Hashtable(); 40 /// <summary> 41 /// 結婚紀念日名稱 42 /// </summary> 43 private static Hashtable weddingNameList = new Hashtable(); 44 45 46 47 private static int currentWeek = 0;//记录周数 48 49 /// <summary> 50 /// 51 /// </summary> 52 private static string[] JQ = { 53 "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑" 54 , "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" 55 }; 56 /// <summary> 57 /// 58 /// </summary> 59 private static int[] JQData = { 60 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 61 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 62 462224, 483532, 504758 63 }; 64 /// <summary> 65 /// 66 /// </summary> 67 static ChineseDate() 68 { 69 //公历节日 70 gHoliday.Add("0101", "元旦"); 71 gHoliday.Add("0114", "日记情人节(Diary Day) ");//Diary Day 日记情人节 72 gHoliday.Add("0202", "世界湿地日 "); 73 gHoliday.Add("0210", "国际气象节 "); 74 gHoliday.Add("0214", "圣瓦伦丁节(Valentine’s Day ),塗聚文 ");//Valentine’s Day 传统情人节 75 76 gHoliday.Add("0301", "国际海豹日 "); 77 gHoliday.Add("0303", "全国爱耳日 "); 78 79 gHoliday.Add("0305", "雷锋日 "); 80 gHoliday.Add("0308", "妇女节 "); 81 gHoliday.Add("0312", "植树节 "); 82 gHoliday.Add("0314", "白色情人节(White Day ),国际警察日 ");//White Day 白色情人节 83 gHoliday.Add("0315", "消费者权益日 "); 84 gHoliday.Add("0317", "中国国医节 国际航海日 "); 85 gHoliday.Add("0321", "世界森林日 消除种族歧视国际日 世界儿歌日 "); 86 gHoliday.Add("0322", "世界水日 "); 87 gHoliday.Add("0323", "世界气象日 "); 88 gHoliday.Add("0324", "世界防治结核病日 "); 89 gHoliday.Add("0325", "全国中小学生安全教育日 "); 90 gHoliday.Add("0330", "巴勒斯坦国土日 "); 91 92 gHoliday.Add("0401", "愚人节 全国爱国卫生运动月(四月) 税收宣传月(四月) "); 93 gHoliday.Add("0414", "黑色情人节(Black Day) ");//Black Day 黑色情人节 94 gHoliday.Add("0407", "世界卫生日 "); 95 gHoliday.Add("0422", "世界地球日 "); 96 gHoliday.Add("0423", "世界图书和版权日 "); 97 gHoliday.Add("0424", "亚非新闻工作者日 "); 98 99 100 gHoliday.Add("0501", "劳动节 "); 101 gHoliday.Add("0504", "青年节 "); 102 gHoliday.Add("0505", "碘缺乏病防治日 "); 103 gHoliday.Add("0508", "世界红十字日 "); 104 gHoliday.Add("0512", "国际护士节 "); 105 gHoliday.Add("0514", "玫瑰情人节(Yellow &Rose Day) ");//Yellow &Rose Day 玫瑰情人节 106 gHoliday.Add("0515", "国际家庭日 "); 107 gHoliday.Add("0517", "国际电信日 "); 108 gHoliday.Add("0518", "国际博物馆日 "); 109 gHoliday.Add("0519", "中国旅游日 "); 110 gHoliday.Add("0520", "全国学生营养日 中国旅游日 "); 111 gHoliday.Add("0523", "国际牛奶日 "); 112 gHoliday.Add("0531", "世界无烟日 "); 113 114 115 gHoliday.Add("0601", "国际儿童节 "); 116 gHoliday.Add("0605", "世界环境保护日 "); 117 gHoliday.Add("0606", "全国爱眼日 "); 118 gHoliday.Add("0614", "亲亲情人节(Kiss Day) ");//Kiss Day 亲亲情人节 119 gHoliday.Add("0617", "防治荒漠化和干旱日 "); 120 gHoliday.Add("0623", "国际奥林匹克日 "); 121 gHoliday.Add("0625", "全国土地日 "); 122 gHoliday.Add("0626", "国际禁毒日 "); 123 124 125 126 gHoliday.Add("0701", "建党节 香港回归纪念日 中共诞辰 世界建筑日 "); 127 gHoliday.Add("0702", "国际体育记者日 "); 128 gHoliday.Add("0707", "抗日战争纪念日 "); 129 gHoliday.Add("0711", "世界人口日 "); 130 gHoliday.Add("0714", "绿色情人节(Green Day) ");//Green Day 绿色情人节 131 gHoliday.Add("0730", "非洲妇女日 "); 132 133 gHoliday.Add("0801", "建军节 "); 134 gHoliday.Add("0808", "中国男子节(爸爸节) "); 135 gHoliday.Add("0814", "银色情人节(Silver Day) ");//Silver Day 银色情人节 136 gHoliday.Add("0815", "抗日战争胜利纪念 "); 137 138 gHoliday.Add("0908", "国际扫盲日 国际新闻工作者日 "); 139 gHoliday.Add("0909", "毛主席逝世纪念 "); 140 gHoliday.Add("0910", "教师节 "); 141 gHoliday.Add("0914", "相片情人节(Music &Photo Day) 世界清洁地球日 ");//Music &Photo Day 相片情人节 142 gHoliday.Add("0916", "国际臭氧层保护日 "); 143 gHoliday.Add("0918", "九·一八事变纪念日 "); 144 gHoliday.Add("0920", "国际爱牙日 "); 145 gHoliday.Add("0927", "世界旅游日 "); 146 gHoliday.Add("0928", "孔子诞辰 "); 147 148 gHoliday.Add("1001", "国庆节 世界音乐日 国际老人节 "); 149 gHoliday.Add("1004", "世界动物日 "); 150 gHoliday.Add("1006", "老人节 "); 151 gHoliday.Add("1008", "全国高血压日 世界视觉日 "); 152 gHoliday.Add("1009", "世界邮政日 万国邮联日 "); 153 gHoliday.Add("1010", "辛亥革命纪念日 世界精神卫生日 "); 154 gHoliday.Add("1013", "世界保健日 国际教师节 "); 155 gHoliday.Add("1014", "葡萄酒情人节(Wine Day) 世界标准日 ");//Wine Day 葡萄酒情人节 156 gHoliday.Add("1015", "国际盲人节(白手杖节) "); 157 gHoliday.Add("1016", "世界粮食日 "); 158 gHoliday.Add("1017", "世界消除贫困日 "); 159 gHoliday.Add("1022", "世界传统医药日 "); 160 gHoliday.Add("1024", "联合国日 "); 161 gHoliday.Add("1031", "世界勤俭日 "); 162 163 gHoliday.Add("1107", "十月社会主义革命纪念日 "); 164 gHoliday.Add("1108", "中国记者日 "); 165 gHoliday.Add("1109", "全国消防安全宣传教育日 "); 166 gHoliday.Add("1110", "世界青年节 "); 167 gHoliday.Add("1111", "国际科学与和平周(本日所属的一周) "); 168 gHoliday.Add("1112", "孙中山诞辰纪念日 "); 169 gHoliday.Add("1114", "电影情人节(Orange & Movie Day) 世界糖尿病日 ");//Orange & Movie Day 电影情人节 170 gHoliday.Add("1117", "国际大学生节 世界学生节 "); 171 gHoliday.Add("1120", "彝族年 "); 172 gHoliday.Add("1121", "彝族年 世界问候日 世界电视日 "); 173 gHoliday.Add("1122", "彝族年 "); 174 gHoliday.Add("1129", "国际声援巴勒斯坦人民国际日 "); 175 176 gHoliday.Add("1201", "世界艾滋病日 "); 177 gHoliday.Add("1203", "世界残疾人日 "); 178 gHoliday.Add("1205", "国际经济和社会发展志愿人员日 "); 179 gHoliday.Add("1208", "国际儿童电视日 "); 180 gHoliday.Add("1209", "世界足球日 "); 181 gHoliday.Add("1210", "世界人权日 "); 182 gHoliday.Add("1212", "西安事变纪念日 "); 183 gHoliday.Add("1213", "南京大屠杀(1937年)纪念日!紧记血泪史! "); 184 gHoliday.Add("1214", "拥抱情人节(Hug Day) ");//Hug Day 拥抱情人节 185 gHoliday.Add("1220", "澳门回归纪念 "); 186 gHoliday.Add("1221", "国际篮球日 "); 187 gHoliday.Add("1224", "平安夜 "); 188 gHoliday.Add("1225", "圣诞节 "); 189 gHoliday.Add("1226", "毛主席诞辰纪念 "); 190 191 192 193 194 195 196 197 198 199 //农历节日(年份數據不正確,只做測試) 200 nHoliday.Add("0101", "0028春节"); 201 nHoliday.Add("0115", "0026元宵节"); 202 nHoliday.Add("0202", "0027龙抬头节"); 203 nHoliday.Add("0505", "0021端午节"); 204 nHoliday.Add("0707", "0820七夕(乞巧节)");// 205 nHoliday.Add("0715", "0020中元节(鬼节)");// 206 nHoliday.Add("0815", "0023中秋节"); 207 nHoliday.Add("0909", "0026重阳节"); 208 nHoliday.Add("1001", "0021寒衣节"); 209 nHoliday.Add("1208", "0028腊八节"); 210 nHoliday.Add("1223", "0120灶君(祭灶)节");// 211 nHoliday.Add("1224", "0220小年"); 212 nHoliday.Add("1227", "1976塗聚文"); 213 214 215 216 //周節日 217 weekHolidays.Add("050201", "1907母亲节Mother's Day"); 218 weekHolidays.Add("050301", "1990全国助残日"); 219 weekHolidays.Add("060301", "1910父亲节"); 220 weekHolidays.Add("090303", "1981国际和平日"); 221 weekHolidays.Add("090401", "1958国际聋人节"); 222 weekHolidays.Add("100102", "1985国际住房日"); 223 weekHolidays.Add("100104", "1987国际减轻自然灾害日"); 224 weekHolidays.Add("110405", "1941感恩节Thanksgiving Day"); 225 //weekHolidays.Add("", ""); 226 227 228 //年中第幾日節日 229 inYearDayHolidays.Add("256", "2002程序员节(День программиста)"); 230 231 232 //指定節日 233 specifiedHolidays.Add("20000423", "復活節(西欧)"); 234 specifiedHolidays.Add("20000430", "復活節(东正教)"); 235 specifiedHolidays.Add("20010415", "復活節"); 236 specifiedHolidays.Add("20020331", "復活節(西歐)"); 237 specifiedHolidays.Add("20020505", "復活節(東正教)"); 238 specifiedHolidays.Add("20030420", "復活節(西歐)"); 239 specifiedHolidays.Add("20030427", "復活節(東正教)"); 240 specifiedHolidays.Add("20040411", "復活節"); 241 specifiedHolidays.Add("20050327", "復活節(西歐)"); 242 specifiedHolidays.Add("20050501", "復活節(東正教)"); 243 specifiedHolidays.Add("20060416", "復活節(西歐)"); 244 specifiedHolidays.Add("20060423", "復活節(東正教)"); 245 specifiedHolidays.Add("20070408", "復活節"); 246 specifiedHolidays.Add("20080323", "復活節(西歐)"); 247 specifiedHolidays.Add("20080427", "復活節(東正教)"); 248 specifiedHolidays.Add("20090412", "復活節(西歐)"); 249 specifiedHolidays.Add("20090419", "復活節(東正教)"); 250 specifiedHolidays.Add("20100404", "復活節"); 251 specifiedHolidays.Add("20110424", "復活節"); 252 specifiedHolidays.Add("20120408", "復活節(西歐)"); 253 specifiedHolidays.Add("20120415", "復活節(東正教)"); 254 specifiedHolidays.Add("20130331", "復活節(西歐)"); 255 specifiedHolidays.Add("20130505", "復活節(東正教)"); 256 specifiedHolidays.Add("20140420", "復活節"); 257 specifiedHolidays.Add("20150405", "復活節(西歐)"); 258 specifiedHolidays.Add("20150412", "復活節(東正教)"); 259 specifiedHolidays.Add("20160327", "復活節(西歐)"); 260 specifiedHolidays.Add("20160501", "復活節(東正教)"); 261 specifiedHolidays.Add("20170416", "復活節"); 262 specifiedHolidays.Add("20180401", "復活節(西歐)"); 263 specifiedHolidays.Add("20190421", "復活節(西歐)"); 264 specifiedHolidays.Add("20190428", "復活節(東正教)"); 265 specifiedHolidays.Add("20200412", "復活節(西歐)"); 266 specifiedHolidays.Add("20200419", "復活節(東正教)"); 267 //specifiedHolidays.Add("", ""); 268 269 270 //specifiedHolidays.Add("", ""); 271 // 結婚紀念日名稱 272 weddingHolidays.Add("0928", "2002劉軍與天金"); 273 weddingHolidays.Add("0115", "2003阿文與阿紅"); 274 275 276 weddingNameList.Add(1, "紙婚(Paper Wedding)"); 277 weddingNameList.Add(2, "布婚/棉布婚(Calico Wedding/Cotton Wedding)"); 278 weddingNameList.Add(3, "羊布婚/皮婚(Muslim Wedding/Leather Wedding)"); 279 weddingNameList.Add(4, "絲婚(Silk Wedding)"); 280 weddingNameList.Add(5, "木婚(Wood Wedding)"); 281 weddingNameList.Add(6, "鐵婚(Iron Wedding)"); 282 weddingNameList.Add(7, "銅婚(Copper Wedding)"); 283 weddingNameList.Add(8, "陶器婚(Pottery Wedding)"); 284 weddingNameList.Add(9, "柳婚(Willow Wedding)"); 285 weddingNameList.Add(10, "錫婚(Tin Wedding)"); 286 weddingNameList.Add(11, "鋼婚(Steel Wedding)"); 287 weddingNameList.Add(12, "鏈婚(Chain Wedding)"); 288 weddingNameList.Add(13, "花邊婚(Lace Wedding)"); 289 weddingNameList.Add(14, "象牙婚(Ivory Wedding)"); 290 weddingNameList.Add(15, "水晶婚(Crystal Wedding)"); 291 weddingNameList.Add(20, "搪瓷婚(China Wedding)"); 292 weddingNameList.Add(25, "銀婚(Silver Wedding/Silver Jubilee)"); 293 weddingNameList.Add(30, "珍珠婚(Pearl Wedding)"); 294 weddingNameList.Add(35, "珊瑚婚/碧玉婚(Coral Wedding/Jade Wedding)"); 295 weddingNameList.Add(40, "紅寶石婚(Ruby Wedding)"); 296 weddingNameList.Add(45, "藍寶石婚(Sapphire Wedding)"); 297 weddingNameList.Add(50, "金婚(Golden Wedding)"); 298 weddingNameList.Add(55, "翡翠婚(Emerald Wedding)"); 299 weddingNameList.Add(60, "鑽石婚(Diamond Wedding/Diamond Jubilee)"); 300 } 301 302 /// <summary> 303 /// 获取农历 304 /// </summary> 305 /// <param name="dt"></param> 306 /// <returns></returns> 307 public static string GetChinaDate(DateTime dt) 308 { 309 if (dt > china.MaxSupportedDateTime || dt < china.MinSupportedDateTime) 310 { 311 //日期范围:1901 年 2 月 19 日 - 2049 年 12 月 30 日 312 throw new Exception(string.Format("日期超出范围!必须在{0}到{1}之间!", 313 china.MinSupportedDateTime.ToString("yyyy-MM-dd"), 314 china.MaxSupportedDateTime.ToString("yyyy-MM-dd"))); 315 } 316 string str = string.Format("{0} {1}{2}", GetYear(dt), GetMonth(dt), GetDay(dt)); 317 string strJQ = GetSolarTerm(dt); 318 if (strJQ != "") 319 { 320 str += " (" + strJQ + ")"; 321 } 322 string strHoliday = GetHoliday(dt); 323 if (strHoliday != "") 324 { 325 str += " " + strHoliday; 326 } 327 string strChinaHoliday = GetChinaHoliday(dt); 328 if (strChinaHoliday != "") 329 { 330 str += " " + strChinaHoliday; 331 } 332 333 return str; 334 } 335 336 /// <summary> 337 /// 获取农历年份 338 /// </summary> 339 /// <param name="dt"></param> 340 /// <returns></returns> 341 public static string GetYear(DateTime dt) 342 { 343 int yearIndex = china.GetSexagenaryYear(dt); 344 string yearTG = " 甲乙丙丁戊己庚辛壬癸"; 345 string yearDZ = " 子丑寅卯辰巳午未申酉戌亥"; 346 string yearSX = " 鼠牛虎兔龙蛇马羊猴鸡狗猪"; 347 int year = china.GetYear(dt); 348 int yTG = china.GetCelestialStem(yearIndex); 349 int yDZ = china.GetTerrestrialBranch(yearIndex); 350 351 string str = string.Format("[{1}]{2}{3}{0}", year, yearSX[yDZ], yearTG[yTG], yearDZ[yDZ]); 352 return str; 353 } 354 355 /// <summary> 356 /// 获取农历月份 357 /// </summary> 358 /// <param name="dt"></param> 359 /// <returns></returns> 360 public static string GetMonth(DateTime dt) 361 { 362 int year = china.GetYear(dt); 363 int iMonth = china.GetMonth(dt); 364 int leapMonth = china.GetLeapMonth(year); 365 bool isLeapMonth = iMonth == leapMonth; 366 if (leapMonth != 0 && iMonth >= leapMonth) 367 { 368 iMonth--; 369 } 370 371 string szText = "正二三四五六七八九十"; 372 string strMonth = isLeapMonth ? "闰" : ""; 373 if (iMonth <= 10) 374 { 375 strMonth += szText.Substring(iMonth - 1, 1); 376 } 377 else if (iMonth == 11) 378 { 379 strMonth += "十一"; 380 } 381 else 382 { 383 strMonth += "腊"; 384 } 385 return strMonth + "月"; 386 } 387 /// <summary> 388 /// 數字農曆月份 389 /// </summary> 390 /// <param name="dt"></param> 391 /// <returns></returns> 392 public static int GetMonthInt(DateTime dt) 393 { 394 int year = china.GetYear(dt); 395 int iMonth = china.GetMonth(dt); 396 //闰月 397 int leapMonth = china.GetLeapMonth(year); 398 bool isLeapMonth = iMonth == leapMonth; 399 if (leapMonth != 0 && iMonth >= leapMonth) 400 { 401 iMonth--; 402 } 403 return iMonth; 404 } 405 406 407 /// <summary> 408 /// 获取农历日期 409 /// </summary> 410 /// <param name="dt"></param> 411 /// <returns></returns> 412 public static string GetDay(DateTime dt) 413 { 414 int iDay = china.GetDayOfMonth(dt); 415 string szText1 = "初十廿三"; 416 string szText2 = "一二三四五六七八九十"; 417 string strDay; 418 if (iDay == 20) 419 { 420 strDay = "二十"; 421 } 422 else if (iDay == 30) 423 { 424 strDay = "三十"; 425 } 426 else 427 { 428 strDay = szText1.Substring((iDay - 1) / 10, 1); 429 strDay = strDay + szText2.Substring((iDay - 1) % 10, 1); 430 } 431 return strDay; 432 } 433 434 /// <summary> 435 /// 获取节气 436 /// </summary> 437 /// <param name="dt"></param> 438 /// <returns></returns> 439 public static string GetSolarTerm(DateTime dt) 440 { 441 DateTime dtBase = new DateTime(1900, 1, 6, 2, 5, 0); 442 DateTime dtNew; 443 double num; 444 int y; 445 string strReturn = ""; 446 447 y = dt.Year; 448 for (int i = 1; i <= 24; i++) 449 { 450 num = 525948.76 * (y - 1900) + JQData[i - 1]; 451 dtNew = dtBase.AddMinutes(num); 452 if (dtNew.DayOfYear == dt.DayOfYear) 453 { 454 strReturn = JQ[i - 1]; 455 } 456 } 457 458 return strReturn; 459 } 460 461 /// <summary> 462 /// 获取公历节日 463 /// </summary> 464 /// <param name="dt"></param> 465 /// <returns></returns> 466 public static string GetHoliday(DateTime dt) 467 { 468 //int y = dt.Year; 469 string strReturn = ""; 470 //int sy = 0; 471 object g = gHoliday[dt.Month.ToString("00") + dt.Day.ToString("00")]; 472 if (g != null) 473 { 474 strReturn = g.ToString(); 475 476 477 } 478 479 return strReturn; 480 } 481 482 /// <summary> 483 /// 获取农历节日 並獲取周年 484 /// 塗聚文 485 /// </summary> 486 /// <param name="dt">公曆時間</param> 487 /// <returns>返回節日名稱和周年</returns> 488 public static string GetChinaHoliday(DateTime dt) 489 { 490 string strReturn = ""; 491 int year = china.GetYear(dt); 492 int iMonth = china.GetMonth(dt); 493 int leapMonth = china.GetLeapMonth(year); 494 int iDay = china.GetDayOfMonth(dt); 495 int sy = 0; 496 497 498 if (china.GetDayOfYear(dt) == china.GetDaysInYear(year)) 499 { 500 strReturn = "除夕"; 501 } 502 else if (leapMonth != iMonth) 503 { 504 if (leapMonth != 0 && iMonth >= leapMonth) 505 { 506 iMonth--; 507 } 508 object n = nHoliday[iMonth.ToString("00") + iDay.ToString("00")]; 509 510 if (n != null) 511 { 512 if (strReturn == "") 513 { 514 strReturn = n.ToString(); 515 sy = year - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份,並以在此年份這後顯示節日 516 if (sy >= 0) 517 { 518 strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; 519 } 520 else 521 { 522 strReturn = ""; 523 } 524 } 525 else 526 { 527 strReturn += " " + n.ToString(); 528 } 529 } 530 } 531 532 return strReturn; 533 } 534 /// <summary> 535 /// 周期性節日 536 /// </summary> 537 /// <param name="dt">公曆時間</param> 538 /// <returns>返回節日名稱和周年</returns> 539 public static string GetWeekHoliday(DateTime dt) 540 { 541 int y = dt.Year; 542 string strReturn = ""; 543 int sy = 0; 544 int weeks = week(dt); //第幾周 545 int weekday = ConvertDayOfWeek(dt.DayOfWeek);//周幾 546 547 object g = weekHolidays[dt.Month.ToString("00") + weeks.ToString("00")+weekday.ToString("00")]; 548 if (g != null) 549 { 550 strReturn = g.ToString(); 551 sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 552 if (sy >= 0) //必須之後的年份才可以顯示 553 { 554 strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; 555 556 } 557 else 558 { 559 strReturn = string.Empty; 560 561 } 562 563 } 564 return strReturn; 565 566 } 567 /// <summary> 568 ///年中第幾日節日 程序員節 569 /// </summary> 570 /// <param name="dt"></param> 571 /// <returns></returns> 572 public static string GetInYearMonth(DateTime dt) 573 { 574 int y = dt.Year; 575 int sy = 0; 576 string strReturn = ""; 577 int inyear = dt.DayOfYear; 578 object g = inYearDayHolidays[inyear.ToString("000")]; 579 if (g != null) 580 { 581 strReturn = g.ToString(); 582 sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 583 if (sy >= 0) //必須之後的年份才可以顯示 584 { 585 strReturn = strReturn.Substring(4, strReturn.Length - 4) + "〖" + sy.ToString() + "周年〗"; 586 587 } 588 else 589 { 590 strReturn = string.Empty; 591 592 } 593 } 594 return strReturn; 595 596 } 597 /// <summary> 598 /// 指定的節日 599 /// </summary> 600 /// <param name="dt"></param> 601 /// <returns></returns> 602 public static string GetSpecifiedDay(DateTime dt) 603 { 604 string strReturn = ""; 605 object g = specifiedHolidays[dt.Year.ToString("0000") + dt.Month.ToString("00") + dt.Day.ToString("00")]; 606 if (g != null) 607 { 608 strReturn = g.ToString(); 609 } 610 return strReturn; 611 612 } 613 614 /// <summary> 615 /// 結婚紀念日 616 /// </summary> 617 /// <param name="dt"></param> 618 /// <returns></returns> 619 public static string GetWeddingHoliday(DateTime dt) 620 { 621 int y = dt.Year; 622 int sy = 0; 623 string strReturn = ""; 624 string nameRetur = string.Empty; 625 object g = weddingHolidays[dt.Month.ToString("00") + dt.Day.ToString("00")]; 626 if (g != null) 627 { 628 strReturn = g.ToString(); 629 630 sy = y - int.Parse(strReturn.Substring(0, 4)); //取前面四位得到年份 631 if (sy >= 0) //必須之後的年份才可以顯示 632 { 633 strReturn = strReturn.Substring(4, strReturn.Length - 4); 634 object name = weddingNameList[sy]; 635 if (name != null) 636 { 637 nameRetur = "〖" + name.ToString() + "紀念〗"; 638 } 639 else 640 { 641 nameRetur = "〖" + sy.ToString() + "周年結婚紀念〗"; 642 } 643 } 644 else 645 { 646 strReturn = ""; 647 } 648 } 649 return strReturn + nameRetur; 650 651 } 652 /// <summary> 653 /// 将星期几转成数字表示 654 /// </summary> 655 /// <param name="dayOfWeek"></param> 656 /// <returns></returns> 657 private static int ConvertDayOfWeek(DayOfWeek dayOfWeek) 658 { 659 switch (dayOfWeek) 660 { 661 case DayOfWeek.Sunday: 662 return 1; 663 case DayOfWeek.Monday: 664 return 2; 665 case DayOfWeek.Tuesday: 666 return 3; 667 case DayOfWeek.Wednesday: 668 return 4; 669 case DayOfWeek.Thursday: 670 return 5; 671 case DayOfWeek.Friday: 672 return 6; 673 case DayOfWeek.Saturday: 674 return 7; 675 default: 676 return 0; 677 } 678 } 679 /// <summary> 680 /// 当前日期是本月的第几周 681 /// </summary> 682 /// <param name="day"></param> 683 /// <returns></returns> 684 private static int week(DateTime day) 685 { 686 687 string y = day.Year.ToString(); 688 string m = day.Month.ToString(); 689 string s = y + "-" + m + "-1"; 690 691 DateTime dt = DateTime.Parse(s); 692 693 int d = day.Day - dt.Day; 694 int w = 1; 695 for (int i = 1; i <= d; i++) 696 { 697 698 DateTime dt1 = dt.AddDays(i); 699 if (dt1.DayOfWeek == DayOfWeek.Sunday) 700 { 701 w = w + 1; 702 } 703 704 } 705 706 return w; 707 } 708 709 }