查看: 2107|回复: 0

[.NET源码] C# 日历类功能的实例代码

发表于 2018-2-8 08:00:02

C# 日历类的实现代码,具体如下所示:

  1. using System;
  2. namespace DotNet.Utilities
  3. {
  4. /// <summary>
  5. /// 农历属性
  6. /// </summary>
  7. public class CNDate
  8. {
  9. /// <summary>
  10. /// 农历年(整型)
  11. /// </summary>
  12. public int cnIntYear = 0;
  13. /// <summary>
  14. /// 农历月份(整型)
  15. /// </summary>
  16. public int cnIntMonth = 0;
  17. /// <summary>
  18. /// 农历天(整型)
  19. /// </summary>
  20. public int cnIntDay = 0;
  21. /// <summary>
  22. /// 农历年(支干)
  23. /// </summary>
  24. public string cnStrYear = "";
  25. /// <summary>
  26. /// 农历月份(字符)
  27. /// </summary>
  28. public string cnStrMonth = "";
  29. /// <summary>
  30. /// 农历天(字符)
  31. /// </summary>
  32. public string cnStrDay = "";
  33. /// <summary>
  34. /// 农历属象
  35. /// </summary>
  36. public string cnAnm = "";
  37. /// <summary>
  38. /// 二十四节气
  39. /// </summary>
  40. public string cnSolarTerm = "";
  41. /// <summary>
  42. /// 阴历节日
  43. /// </summary>
  44. public string cnFtvl = "";
  45. /// <summary>
  46. /// 阳历节日
  47. /// </summary>
  48. public string cnFtvs = "";
  49. }
  50. /// <summary>
  51. /// 公历转农历
  52. /// </summary>
  53. public class ChinaDate
  54. {
  55. #region 私有方法
  56. private static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554,
  57. 0x056a0, 0x09ad0, 0x055d2, 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0,
  58. 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
  59. 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550,
  60. 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0,
  61. 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263,
  62. 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0,
  63. 0x195a6, 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, 0x04af5,
  64. 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960, 0x0d954, 0x0d4a0,
  65. 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
  66. 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0,
  67. 0x0d260, 0x0ea65, 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520,
  68. 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0 };
  69. private static int[] year20 = new int[] { 1, 4, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
  70. private static int[] year19 = new int[] { 0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
  71. private static int[] year2000 = new int[] { 0, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
  72. private static String[] nStr1 = new String[] { "", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
  73. private static String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
  74. private static String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
  75. private static String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  76. private static String[] solarTerm = new String[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
  77. 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 };
  78. private static String[] lFtv = new String[] { "0101农历春节", "0202 龙抬头节", "0115 元宵节", "0505 端午节", "0707 七夕情人节", "0815 中秋节", "0909 重阳节", "1208 腊八节", "1114 李君先生生日", "1224 小年", "0100除夕" };
  79. private static String[] sFtv = new String[] { "0101 新年元旦",
  80. "0202 世界湿地日",
  81. "0207 国际声援南非日",
  82. "0210 国际气象节",
  83. "0214 情人节",
  84. "0301 国际海豹日",
  85. "0303 全国爱耳日",
  86. "0308 国际妇女节",
  87. "0312 植树节 孙中山逝世纪念日",
  88. "0314 国际警察日",
  89. "0315 国际消费者权益日",
  90. "0317 中国国医节 国际航海日",
  91. "0321 世界森林日 消除种族歧视国际日",
  92. "0321 世界儿歌日",
  93. "0322 世界水日",
  94. "0323 世界气象日",
  95. "0324 世界防治结核病日",
  96. "0325 全国中小学生安全教育日",
  97. "0330 巴勒斯坦国土日",
  98. "0401 愚人节 全国爱国卫生运动月(四月) 税收宣传月(四月)",
  99. "0407 世界卫生日",
  100. "0422 世界地球日",
  101. "0423 世界图书和版权日",
  102. "0424 亚非新闻工作者日",
  103. "0501 国际劳动节",
  104. "0504 中国五四青年节",
  105. "0505 碘缺乏病防治日",
  106. "0508 世界红十字日",
  107. "0512 国际护士节",
  108. "0515 国际家庭日",
  109. "0517 世界电信日",
  110. "0518 国际博物馆日",
  111. "0520 全国学生营养日",
  112. "0523 国际牛奶日",
  113. "0531 世界无烟日",
  114. "0601 国际儿童节",
  115. "0605 世界环境日",
  116. "0606 全国爱眼日",
  117. "0617 防治荒漠化和干旱日",
  118. "0623 国际奥林匹克日",
  119. "0625 全国土地日",
  120. "0626 国际反毒品日",
  121. "0701 中国gongchandang建党日 世界建筑日",
  122. "0702 国际体育记者日",
  123. "0707 中国人民抗日战争纪念日",
  124. "0711 世界人口日",
  125. "0730 非洲妇女日",
  126. "0801 中国建军节",
  127. "0808 中国男子节(爸爸节)",
  128. "0815 日本正式宣布无条件投降日",
  129. "0908 国际扫盲日 国际新闻工作者日",
  130. "0910 教师节",
  131. "0914 世界清洁地球日",
  132. "0916 国际臭氧层保护日",
  133. "0918 九·一八事变纪念日",
  134. "0920 全国爱牙日",
  135. "0927 世界旅游日",
  136. "1001 国庆节 世界音乐日 国际老人节",
  137. "1001 国际音乐日",
  138. "1002 国际和平与民主自由斗争日",
  139. "1004 世界动物日",
  140. "1008 全国高血压日",
  141. "1008 世界视觉日",
  142. "1009 世界邮政日 万国邮联日",
  143. "1010 辛亥革命纪念日 世界精神卫生日",
  144. "1013 世界保健日 国际教师节",
  145. "1014 世界标准日",
  146. "1015 国际盲人节(白手杖节)",
  147. "1016 世界粮食日",
  148. "1017 世界消除贫困日",
  149. "1022 世界传统医药日",
  150. "1024 联合国日 世界发展信息日",
  151. "1031 世界勤俭日",
  152. "1107 十月社会主义革命纪念日",
  153. "1108 中国记者日",
  154. "1109 全国消防安全宣传教育日",
  155. "1110 世界青年节",
  156. "1111 国际科学与和平周(本日所属的一周)",
  157. "1112 孙中山诞辰纪念日",
  158. "1114 世界糖尿病日",
  159. "1117 国际大学生节 世界学生节",
  160. "1121 世界问候日 世界电视日",
  161. "1129 国际声援巴勒斯坦人民国际日",
  162. "1201 世界艾滋病日",
  163. "1203 世界残疾人日",
  164. "1205 国际经济和社会发展志愿人员日",
  165. "1208 国际儿童电视日",
  166. "1209 世界足球日",
  167. "1210 世界人权日",
  168. "1212 西安事变纪念日",
  169. "1213 南京大屠杀(1937年)纪念日!紧记血泪史!",
  170. "1221 国际篮球日",
  171. "1224 平安夜",
  172. "1225 圣诞节",
  173. "1226 毛主席诞辰",
  174. "1229 国际生物多样性日" };
  175. /// <summary>
  176. /// 传回农历y年的总天数
  177. /// </summary>
  178. private static int lYearDays(int y)
  179. {
  180. int i, sum = 348;
  181. for (i = 0x8000; i > 0x8; i >>= 1)
  182. {
  183. if ((lunarInfo[y - 1900] & i) != 0)
  184. sum += 1;
  185. }
  186. return (sum + leapDays(y));
  187. }
  188. /// <summary>
  189. /// 传回农历y年闰月的天数
  190. /// </summary>
  191. private static int leapDays(int y)
  192. {
  193. if (leapMonth(y) != 0)
  194. {
  195. if ((lunarInfo[y - 1900] & 0x10000) != 0)
  196. return 30;
  197. else
  198. return 29;
  199. }
  200. else
  201. return 0;
  202. }
  203. /// <summary>
  204. /// 传回农历y年闰哪个月 1-12 , 没闰传回 0
  205. /// </summary>
  206. private static int leapMonth(int y)
  207. {
  208. return (int)(lunarInfo[y - 1900] & 0xf);
  209. }
  210. /// <summary>
  211. /// 传回农历y年m月的总天数
  212. /// </summary>
  213. private static int monthDays(int y, int m)
  214. {
  215. if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
  216. return 29;
  217. else
  218. return 30;
  219. }
  220. /// <summary>
  221. /// 传回农历y年的生肖
  222. /// </summary>
  223. private static String AnimalsYear(int y)
  224. {
  225. return Animals[(y - 4) % 12];
  226. }
  227. /// <summary>
  228. /// 传入月日的offset 传回干支,0=甲子
  229. /// </summary>
  230. private static String cyclicalm(int num)
  231. {
  232. return (Gan[num % 10] + Zhi[num % 12]);
  233. }
  234. /// <summary>
  235. /// 传入offset 传回干支, 0=甲子
  236. /// </summary>
  237. private static String cyclical(int y)
  238. {
  239. int num = y - 1900 + 36;
  240. return (cyclicalm(num));
  241. }
  242. /// <summary>
  243. /// 传出农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
  244. /// </summary>
  245. private long[] Lunar(int y, int m)
  246. {
  247. long[] nongDate = new long[7];
  248. int i = 0, temp = 0, leap = 0;
  249. DateTime baseDate = new DateTime(1900 + 1900, 2, 31);
  250. DateTime objDate = new DateTime(y + 1900, m + 1, 1);
  251. TimeSpan ts = objDate - baseDate;
  252. long offset = (long)ts.TotalDays;
  253. if (y < 2000)
  254. offset += year19[m - 1];
  255. if (y > 2000)
  256. offset += year20[m - 1];
  257. if (y == 2000)
  258. offset += year2000[m - 1];
  259. nongDate[5] = offset + 40;
  260. nongDate[4] = 14;
  261. for (i = 1900; i < 2050 && offset > 0; i++)
  262. {
  263. temp = lYearDays(i);
  264. offset -= temp;
  265. nongDate[4] += 12;
  266. }
  267. if (offset < 0)
  268. {
  269. offset += temp;
  270. i--;
  271. nongDate[4] -= 12;
  272. }
  273. nongDate[0] = i;
  274. nongDate[3] = i - 1864;
  275. leap = leapMonth(i); // 闰哪个月
  276. nongDate[6] = 0;
  277. for (i = 1; i < 13 && offset > 0; i++)
  278. {
  279. // 闰月
  280. if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
  281. {
  282. --i;
  283. nongDate[6] = 1;
  284. temp = leapDays((int)nongDate[0]);
  285. }
  286. else
  287. {
  288. temp = monthDays((int)nongDate[0], i);
  289. }
  290. // 解除闰月
  291. if (nongDate[6] == 1 && i == (leap + 1))
  292. nongDate[6] = 0;
  293. offset -= temp;
  294. if (nongDate[6] == 0)
  295. nongDate[4]++;
  296. }
  297. if (offset == 0 && leap > 0 && i == leap + 1)
  298. {
  299. if (nongDate[6] == 1)
  300. {
  301. nongDate[6] = 0;
  302. }
  303. else
  304. {
  305. nongDate[6] = 1;
  306. --i;
  307. --nongDate[4];
  308. }
  309. }
  310. if (offset < 0)
  311. {
  312. offset += temp;
  313. --i;
  314. --nongDate[4];
  315. }
  316. nongDate[1] = i;
  317. nongDate[2] = offset + 1;
  318. return nongDate;
  319. }
  320. /// <summary>
  321. /// 传出y年m月d日对应的农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
  322. /// </summary>
  323. private static long[] calElement(int y, int m, int d)
  324. {
  325. long[] nongDate = new long[7];
  326. int i = 0, temp = 0, leap = 0;
  327. DateTime baseDate = new DateTime(1900, 1, 31);
  328. DateTime objDate = new DateTime(y, m, d);
  329. TimeSpan ts = objDate - baseDate;
  330. long offset = (long)ts.TotalDays;
  331. nongDate[5] = offset + 40;
  332. nongDate[4] = 14;
  333. for (i = 1900; i < 2050 && offset > 0; i++)
  334. {
  335. temp = lYearDays(i);
  336. offset -= temp;
  337. nongDate[4] += 12;
  338. }
  339. if (offset < 0)
  340. {
  341. offset += temp;
  342. i--;
  343. nongDate[4] -= 12;
  344. }
  345. nongDate[0] = i;
  346. nongDate[3] = i - 1864;
  347. leap = leapMonth(i); // 闰哪个月
  348. nongDate[6] = 0;
  349. for (i = 1; i < 13 && offset > 0; i++)
  350. {
  351. // 闰月
  352. if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
  353. {
  354. --i;
  355. nongDate[6] = 1;
  356. temp = leapDays((int)nongDate[0]);
  357. }
  358. else
  359. {
  360. temp = monthDays((int)nongDate[0], i);
  361. }
  362. // 解除闰月
  363. if (nongDate[6] == 1 && i == (leap + 1))
  364. nongDate[6] = 0;
  365. offset -= temp;
  366. if (nongDate[6] == 0)
  367. nongDate[4]++;
  368. }
  369. if (offset == 0 && leap > 0 && i == leap + 1)
  370. {
  371. if (nongDate[6] == 1)
  372. {
  373. nongDate[6] = 0;
  374. }
  375. else
  376. {
  377. nongDate[6] = 1;
  378. --i;
  379. --nongDate[4];
  380. }
  381. }
  382. if (offset < 0)
  383. {
  384. offset += temp;
  385. --i;
  386. --nongDate[4];
  387. }
  388. nongDate[1] = i;
  389. nongDate[2] = offset + 1;
  390. return nongDate;
  391. }
  392. private static String getChinaDate(int day)
  393. {
  394. String a = "";
  395. if (day == 10)
  396. return "初十";
  397. if (day == 20)
  398. return "二十";
  399. if (day == 30)
  400. return "三十";
  401. int two = (int)((day) / 10);
  402. if (two == 0)
  403. a = "初";
  404. if (two == 1)
  405. a = "十";
  406. if (two == 2)
  407. a = "廿";
  408. if (two == 3)
  409. a = "三";
  410. int one = (int)(day % 10);
  411. switch (one)
  412. {
  413. case 1:
  414. a += "一";
  415. break;
  416. case 2:
  417. a += "二";
  418. break;
  419. case 3:
  420. a += "三";
  421. break;
  422. case 4:
  423. a += "四";
  424. break;
  425. case 5:
  426. a += "五";
  427. break;
  428. case 6:
  429. a += "六";
  430. break;
  431. case 7:
  432. a += "七";
  433. break;
  434. case 8:
  435. a += "八";
  436. break;
  437. case 9:
  438. a += "九";
  439. break;
  440. }
  441. return a;
  442. }
  443. private static DateTime sTerm(int y, int n)
  444. {
  445. double ms = 31556925974.7 * (y - 1900);
  446. double ms1 = sTermInfo[n];
  447. DateTime offDate = new DateTime(1900, 1, 6, 2, 5, 0);
  448. offDate = offDate.AddMilliseconds(ms);
  449. offDate = offDate.AddMinutes(ms1);
  450. return offDate;
  451. }
  452. static string FormatDate(int m, int d)
  453. {
  454. return string.Format("{0:00}{1:00}", m, d);
  455. }
  456. #endregion
  457. #region 公有方法
  458. /// <summary>
  459. /// 传回公历y年m月的总天数
  460. /// </summary>
  461. public static int GetDaysByMonth(int y, int m)
  462. {
  463. int[] days = new int[] { 31, DateTime.IsLeapYear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  464. return days[m - 1];
  465. }
  466. /// <summary>
  467. /// 根据日期值获得周一的日期
  468. /// </summary>
  469. /// <param name="dt">输入日期</param>
  470. /// <returns>周一的日期</returns>
  471. public static DateTime GetMondayDateByDate(DateTime dt)
  472. {
  473. double d = 0;
  474. switch ((int)dt.DayOfWeek)
  475. {
  476. //case 1: d = 0; break;
  477. case 2: d = -1; break;
  478. case 3: d = -2; break;
  479. case 4: d = -3; break;
  480. case 5: d = -4; break;
  481. case 6: d = -5; break;
  482. case 0: d = -6; break;
  483. }
  484. return dt.AddDays(d);
  485. }
  486. /// <summary>
  487. /// 获取农历
  488. /// </summary>
  489. public static CNDate getChinaDate(DateTime dt)
  490. {
  491. CNDate cd = new CNDate();
  492. int year = dt.Year;
  493. int month = dt.Month;
  494. int date = dt.Day;
  495. long[] l = calElement(year, month, date);
  496. cd.cnIntYear = (int)l[0];
  497. cd.cnIntMonth = (int)l[1];
  498. cd.cnIntDay = (int)l[2];
  499. cd.cnStrYear = cyclical(year);
  500. cd.cnAnm = AnimalsYear(year);
  501. cd.cnStrMonth = nStr1[(int)l[1]];
  502. cd.cnStrDay = getChinaDate((int)(l[2]));
  503. string smd = dt.ToString("MMdd");
  504. string lmd = FormatDate(cd.cnIntMonth, cd.cnIntDay);
  505. for (int i = 0; i < solarTerm.Length; i++)
  506. {
  507. string s1 = sTerm(dt.Year, i).ToString("MMdd");
  508. if (s1.Equals(dt.ToString("MMdd")))
  509. {
  510. cd.cnSolarTerm = solarTerm[i];
  511. break;
  512. }
  513. }
  514. foreach (string s in sFtv)
  515. {
  516. string s1 = s.Substring(0, 4);
  517. if (s1.Equals(smd))
  518. {
  519. cd.cnFtvs = s.Substring(4, s.Length - 4);
  520. break;
  521. }
  522. }
  523. foreach (string s in lFtv)
  524. {
  525. string s1 = s.Substring(0, 4);
  526. if (s1.Equals(lmd))
  527. {
  528. cd.cnFtvl = s.Substring(4, s.Length - 4);
  529. break;
  530. }
  531. }
  532. dt = dt.AddDays(1);
  533. year = dt.Year;
  534. month = dt.Month;
  535. date = dt.Day;
  536. l = calElement(year, month, date);
  537. lmd = FormatDate((int)l[1], (int)l[2]);
  538. if (lmd.Equals("0101")) cd.cnFtvl = "除夕";
  539. return cd;
  540. }
  541. #endregion
  542. }
  543. /// <summary>
  544. /// 中国日历
  545. /// </summary>
  546. //-------------------------------------------------------------------------------
  547. //调用:
  548. //ChineseCalendar c = new ChineseCalendar(new DateTime(1990, 01, 15));
  549. //StringBuilder dayInfo = new StringBuilder();
  550. //dayInfo.Append("阳历:" + c.DateString + "\r\n");
  551. //dayInfo.Append("农历:" + c.ChineseDateString + "\r\n");
  552. //dayInfo.Append("星期:" + c.WeekDayStr);
  553. //dayInfo.Append("时辰:" + c.ChineseHour + "\r\n");
  554. //dayInfo.Append("属相:" + c.AnimalString + "\r\n");
  555. //dayInfo.Append("节气:" + c.ChineseTwentyFourDay + "\r\n");
  556. //dayInfo.Append("前一个节气:" + c.ChineseTwentyFourPrevDay + "\r\n");
  557. //dayInfo.Append("下一个节气:" + c.ChineseTwentyFourNextDay + "\r\n");
  558. //dayInfo.Append("节日:" + c.DateHoliday + "\r\n");
  559. //dayInfo.Append("干支:" + c.GanZhiDateString + "\r\n");
  560. //dayInfo.Append("星宿:" + c.ChineseConstellation + "\r\n");
  561. //dayInfo.Append("星座:" + c.Constellation + "\r\n");
  562. //-------------------------------------------------------------------------------
  563. public class ChineseCalendar
  564. {
  565. #region 内部结构
  566. /// <summary>
  567. /// 阳历
  568. /// </summary>
  569. private struct SolarHolidayStruct
  570. {
  571. public int Month;
  572. public int Day;
  573. public int Recess; //假期长度
  574. public string HolidayName;
  575. public SolarHolidayStruct(int month, int day, int recess, string name)
  576. {
  577. Month = month;
  578. Day = day;
  579. Recess = recess;
  580. HolidayName = name;
  581. }
  582. }
  583. /// <summary>
  584. /// 农历
  585. /// </summary>
  586. private struct LunarHolidayStruct
  587. {
  588. public int Month;
  589. public int Day;
  590. public int Recess;
  591. public string HolidayName;
  592. public LunarHolidayStruct(int month, int day, int recess, string name)
  593. {
  594. Month = month;
  595. Day = day;
  596. Recess = recess;
  597. HolidayName = name;
  598. }
  599. }
  600. private struct WeekHolidayStruct
  601. {
  602. public int Month;
  603. public int WeekAtMonth;
  604. public int WeekDay;
  605. public string HolidayName;
  606. public WeekHolidayStruct(int month, int weekAtMonth, int weekDay, string name)
  607. {
  608. Month = month;
  609. WeekAtMonth = weekAtMonth;
  610. WeekDay = weekDay;
  611. HolidayName = name;
  612. }
  613. }
  614. #endregion
  615. #region 内部变量
  616. private DateTime _date;
  617. private DateTime _datetime;
  618. private int _cYear;
  619. private int _cMonth;
  620. private int _cDay;
  621. private bool _cIsLeapMonth; //当月是否闰月
  622. private bool _cIsLeapYear; //当年是否有闰月
  623. #endregion
  624. #region 基础数据
  625. #region 基本常量
  626. private const int MinYear = 1900;
  627. private const int MaxYear = 2050;
  628. private static DateTime MinDay = new DateTime(1900, 1, 30);
  629. private static DateTime MaxDay = new DateTime(2049, 12, 31);
  630. private const int GanZhiStartYear = 1864; //干支计算起始年
  631. private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22);//起始日
  632. private const string HZNum = "零一二三四五六七八九";
  633. private const int AnimalStartYear = 1900; //1900年为鼠年
  634. private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13);//28星宿参考值,本日为角
  635. #endregion
  636. #region 阴历数据
  637. /// <summary>
  638. /// 来源于网上的农历数据
  639. /// </summary>
  640. /// <remarks>
  641. /// 数据结构如下,共使用17位数据
  642. /// 第17位:表示闰月天数,0表示29天 1表示30天
  643. /// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
  644. /// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
  645. ///</remarks>
  646. private static int[] LunarDateArray = new int[]{
  647. 0x04BD8,0x04AE0,0x0A570,0x054D5,0x0D260,0x0D950,0x16554,0x056A0,0x09AD0,0x055D2,
  648. 0x04AE0,0x0A5B6,0x0A4D0,0x0D250,0x1D255,0x0B540,0x0D6A0,0x0ADA2,0x095B0,0x14977,
  649. 0x04970,0x0A4B0,0x0B4B5,0x06A50,0x06D40,0x1AB54,0x02B60,0x09570,0x052F2,0x04970,
  650. 0x06566,0x0D4A0,0x0EA50,0x06E95,0x05AD0,0x02B60,0x186E3,0x092E0,0x1C8D7,0x0C950,
  651. 0x0D4A0,0x1D8A6,0x0B550,0x056A0,0x1A5B4,0x025D0,0x092D0,0x0D2B2,0x0A950,0x0B557,
  652. 0x06CA0,0x0B550,0x15355,0x04DA0,0x0A5B0,0x14573,0x052B0,0x0A9A8,0x0E950,0x06AA0,
  653. 0x0AEA6,0x0AB50,0x04B60,0x0AAE4,0x0A570,0x05260,0x0F263,0x0D950,0x05B57,0x056A0,
  654. 0x096D0,0x04DD5,0x04AD0,0x0A4D0,0x0D4D4,0x0D250,0x0D558,0x0B540,0x0B6A0,0x195A6,
  655. 0x095B0,0x049B0,0x0A974,0x0A4B0,0x0B27A,0x06A50,0x06D40,0x0AF46,0x0AB60,0x09570,
  656. 0x04AF5,0x04970,0x064B0,0x074A3,0x0EA50,0x06B58,0x055C0,0x0AB60,0x096D5,0x092E0,
  657. 0x0C960,0x0D954,0x0D4A0,0x0DA50,0x07552,0x056A0,0x0ABB7,0x025D0,0x092D0,0x0CAB5,
  658. 0x0A950,0x0B4A0,0x0BAA4,0x0AD50,0x055D9,0x04BA0,0x0A5B0,0x15176,0x052B0,0x0A930,
  659. 0x07954,0x06AA0,0x0AD50,0x05B52,0x04B60,0x0A6E6,0x0A4E0,0x0D260,0x0EA65,0x0D530,
  660. 0x05AA0,0x076A3,0x096D0,0x04BD7,0x04AD0,0x0A4D0,0x1D0B6,0x0D250,0x0D520,0x0DD45,
  661. 0x0B5A0,0x056D0,0x055B2,0x049B0,0x0A577,0x0A4B0,0x0AA50,0x1B255,0x06D20,0x0ADA0,
  662. 0x14B63
  663. };
  664. #endregion
  665. #region 星座名称
  666. private static string[] _constellationName =
  667. {
  668. "白羊座", "金牛座", "双子座",
  669. "巨蟹座", "狮子座", "处女座",
  670. "天秤座", "天蝎座", "射手座",
  671. "摩羯座", "水瓶座", "双鱼座"
  672. };
  673. #endregion
  674. #region 二十四节气
  675. private static string[] _lunarHolidayName =
  676. {
  677. "小寒", "大寒", "立春", "雨水",
  678. "惊蛰", "春分", "清明", "谷雨",
  679. "立夏", "小满", "芒种", "夏至",
  680. "小暑", "大暑", "立秋", "处暑",
  681. "白露", "秋分", "寒露", "霜降",
  682. "立冬", "小雪", "大雪", "冬至"
  683. };
  684. #endregion
  685. #region 二十八星宿
  686. private static string[] _chineseConstellationName =
  687. {
  688. //四 五 六 日 一 二 三
  689. "角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹",
  690. "斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝",
  691. "奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿",
  692. "井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","轸水蚓"
  693. };
  694. #endregion
  695. #region 节气数据
  696. private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" };
  697. 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 };
  698. #endregion
  699. #region 农历相关数据
  700. private static string ganStr = "甲乙丙丁戊己庚辛壬癸";
  701. private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥";
  702. private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
  703. private static string nStr1 = "日一二三四五六七八九";
  704. private static string nStr2 = "初十廿卅";
  705. private static string[] _monthString =
  706. {
  707. "出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"
  708. };
  709. #endregion
  710. #region 按公历计算的节日
  711. private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{
  712. new SolarHolidayStruct(1, 1, 1, "元旦"),
  713. new SolarHolidayStruct(2, 2, 0, "世界湿地日"),
  714. new SolarHolidayStruct(2, 10, 0, "国际气象节"),
  715. new SolarHolidayStruct(2, 14, 0, "情人节"),
  716. new SolarHolidayStruct(3, 1, 0, "国际海豹日"),
  717. new SolarHolidayStruct(3, 5, 0, "学雷锋纪念日"),
  718. new SolarHolidayStruct(3, 8, 0, "妇女节"),
  719. new SolarHolidayStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),
  720. new SolarHolidayStruct(3, 14, 0, "国际警察日"),
  721. new SolarHolidayStruct(3, 15, 0, "消费者权益日"),
  722. new SolarHolidayStruct(3, 17, 0, "中国国医节 国际航海日"),
  723. new SolarHolidayStruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日"),
  724. new SolarHolidayStruct(3, 22, 0, "世界水日"),
  725. new SolarHolidayStruct(3, 24, 0, "世界防治结核病日"),
  726. new SolarHolidayStruct(4, 1, 0, "愚人节"),
  727. new SolarHolidayStruct(4, 7, 0, "世界卫生日"),
  728. new SolarHolidayStruct(4, 22, 0, "世界地球日"),
  729. new SolarHolidayStruct(5, 1, 1, "劳动节"),
  730. new SolarHolidayStruct(5, 2, 1, "劳动节假日"),
  731. new SolarHolidayStruct(5, 3, 1, "劳动节假日"),
  732. new SolarHolidayStruct(5, 4, 0, "青年节"),
  733. new SolarHolidayStruct(5, 8, 0, "世界红十字日"),
  734. new SolarHolidayStruct(5, 12, 0, "国际护士节"),
  735. new SolarHolidayStruct(5, 31, 0, "世界无烟日"),
  736. new SolarHolidayStruct(6, 1, 0, "国际儿童节"),
  737. new SolarHolidayStruct(6, 5, 0, "世界环境保护日"),
  738. new SolarHolidayStruct(6, 26, 0, "国际禁毒日"),
  739. new SolarHolidayStruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日"),
  740. new SolarHolidayStruct(7, 11, 0, "世界人口日"),
  741. new SolarHolidayStruct(8, 1, 0, "建军节"),
  742. new SolarHolidayStruct(8, 8, 0, "中国男子节 父亲节"),
  743. new SolarHolidayStruct(8, 15, 0, "抗日战争胜利纪念"),
  744. new SolarHolidayStruct(9, 9, 0, "毛主席逝世纪念"),
  745. new SolarHolidayStruct(9, 10, 0, "教师节"),
  746. new SolarHolidayStruct(9, 18, 0, "九·一八事变纪念日"),
  747. new SolarHolidayStruct(9, 20, 0, "国际爱牙日"),
  748. new SolarHolidayStruct(9, 27, 0, "世界旅游日"),
  749. new SolarHolidayStruct(9, 28, 0, "孔子诞辰"),
  750. new SolarHolidayStruct(10, 1, 1, "国庆节 国际音乐日"),
  751. new SolarHolidayStruct(10, 2, 1, "国庆节假日"),
  752. new SolarHolidayStruct(10, 3, 1, "国庆节假日"),
  753. new SolarHolidayStruct(10, 6, 0, "老人节"),
  754. new SolarHolidayStruct(10, 24, 0, "联合国日"),
  755. new SolarHolidayStruct(11, 10, 0, "世界青年节"),
  756. new SolarHolidayStruct(11, 12, 0, "孙中山诞辰纪念"),
  757. new SolarHolidayStruct(12, 1, 0, "世界艾滋病日"),
  758. new SolarHolidayStruct(12, 3, 0, "世界残疾人日"),
  759. new SolarHolidayStruct(12, 20, 0, "澳门回归纪念"),
  760. new SolarHolidayStruct(12, 24, 0, "平安夜"),
  761. new SolarHolidayStruct(12, 25, 0, "圣诞节"),
  762. new SolarHolidayStruct(12, 26, 0, "毛主席诞辰纪念")
  763. };
  764. #endregion
  765. #region 按农历计算的节日
  766. private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{
  767. new LunarHolidayStruct(1, 1, 1, "春节"),
  768. new LunarHolidayStruct(1, 15, 0, "元宵节"),
  769. new LunarHolidayStruct(5, 5, 0, "端午节"),
  770. new LunarHolidayStruct(7, 7, 0, "七夕情人节"),
  771. new LunarHolidayStruct(7, 15, 0, "中元节 盂兰盆节"),
  772. new LunarHolidayStruct(8, 15, 0, "中秋节"),
  773. new LunarHolidayStruct(9, 9, 0, "重阳节"),
  774. new LunarHolidayStruct(12, 8, 0, "腊八节"),
  775. new LunarHolidayStruct(12, 23, 0, "北方小年(扫房)"),
  776. new LunarHolidayStruct(12, 24, 0, "南方小年(掸尘)"),
  777. //new LunarHolidayStruct(12, 30, 0, "除夕") //注意除夕需要其它方法进行计算
  778. };
  779. #endregion
  780. #region 按某月第几个星期几
  781. private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{
  782. new WeekHolidayStruct(5, 2, 1, "母亲节"),
  783. new WeekHolidayStruct(5, 3, 1, "全国助残日"),
  784. new WeekHolidayStruct(6, 3, 1, "父亲节"),
  785. new WeekHolidayStruct(9, 3, 3, "国际和平日"),
  786. new WeekHolidayStruct(9, 4, 1, "国际聋人节"),
  787. new WeekHolidayStruct(10, 1, 2, "国际住房日"),
  788. new WeekHolidayStruct(10, 1, 4, "国际减轻自然灾害日"),
  789. new WeekHolidayStruct(11, 4, 5, "感恩节")
  790. };
  791. #endregion
  792. #endregion
  793. #region 构造函数
  794. #region 公历日期初始化
  795. /// <summary>
  796. /// 用一个标准的公历日期来初使化
  797. /// </summary>
  798. public ChineseCalendar(DateTime dt)
  799. {
  800. int i;
  801. int leap;
  802. int temp;
  803. int offset;
  804. CheckDateLimit(dt);
  805. _date = dt.Date;
  806. _datetime = dt;
  807. //农历日期计算部分
  808. leap = 0;
  809. temp = 0;
  810. //计算两天的基本差距
  811. TimeSpan ts = _date - ChineseCalendar.MinDay;
  812. offset = ts.Days;
  813. for (i = MinYear; i <= MaxYear; i++)
  814. {
  815. //求当年农历年天数
  816. temp = GetChineseYearDays(i);
  817. if (offset - temp < 1)
  818. break;
  819. else
  820. {
  821. offset = offset - temp;
  822. }
  823. }
  824. _cYear = i;
  825. //计算该年闰哪个月
  826. leap = GetChineseLeapMonth(_cYear);
  827. //设定当年是否有闰月
  828. if (leap > 0)
  829. {
  830. _cIsLeapYear = true;
  831. }
  832. else
  833. {
  834. _cIsLeapYear = false;
  835. }
  836. _cIsLeapMonth = false;
  837. for (i = 1; i <= 12; i++)
  838. {
  839. //闰月
  840. if ((leap > 0) && (i == leap + 1) && (_cIsLeapMonth == false))
  841. {
  842. _cIsLeapMonth = true;
  843. i = i - 1;
  844. temp = GetChineseLeapMonthDays(_cYear); //计算闰月天数
  845. }
  846. else
  847. {
  848. _cIsLeapMonth = false;
  849. temp = GetChineseMonthDays(_cYear, i); //计算非闰月天数
  850. }
  851. offset = offset - temp;
  852. if (offset <= 0) break;
  853. }
  854. offset = offset + temp;
  855. _cMonth = i;
  856. _cDay = offset;
  857. }
  858. #endregion
  859. #region 农历日期初始化
  860. /// <summary>
  861. /// 用农历的日期来初使化
  862. /// </summary>
  863. /// <param name="cy">农历年</param>
  864. /// <param name="cm">农历月</param>
  865. /// <param name="cd">农历日</param>
  866. /// <param name="LeapFlag">闰月标志</param>
  867. public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
  868. {
  869. int i, leap, Temp, offset;
  870. CheckChineseDateLimit(cy, cm, cd, leapMonthFlag);
  871. _cYear = cy;
  872. _cMonth = cm;
  873. _cDay = cd;
  874. offset = 0;
  875. for (i = MinYear; i < cy; i++)
  876. {
  877. //求当年农历年天数
  878. Temp = GetChineseYearDays(i);
  879. offset = offset + Temp;
  880. }
  881. //计算该年应该闰哪个月
  882. leap = GetChineseLeapMonth(cy);
  883. if (leap != 0)
  884. {
  885. this._cIsLeapYear = true;
  886. }
  887. else
  888. {
  889. this._cIsLeapYear = false;
  890. }
  891. if (cm != leap)
  892. {
  893. //当前日期并非闰月
  894. _cIsLeapMonth = false;
  895. }
  896. else
  897. {
  898. //使用用户输入的是否闰月月份
  899. _cIsLeapMonth = leapMonthFlag;
  900. }
  901. //当年没有闰月||计算月份小于闰月
  902. if ((_cIsLeapYear == false) || (cm < leap))
  903. {
  904. for (i = 1; i < cm; i++)
  905. {
  906. Temp = GetChineseMonthDays(cy, i);//计算非闰月天数
  907. offset = offset + Temp;
  908. }
  909. //检查日期是否大于最大天
  910. if (cd > GetChineseMonthDays(cy, cm))
  911. {
  912. throw new Exception("不合法的农历日期");
  913. }
  914. //加上当月的天数
  915. offset = offset + cd;
  916. }
  917. //是闰年,且计算月份大于或等于闰月
  918. else
  919. {
  920. for (i = 1; i < cm; i++)
  921. {
  922. //计算非闰月天数
  923. Temp = GetChineseMonthDays(cy, i);
  924. offset = offset + Temp;
  925. }
  926. //计算月大于闰月
  927. if (cm > leap)
  928. {
  929. Temp = GetChineseLeapMonthDays(cy); //计算闰月天数
  930. offset = offset + Temp; //加上闰月天数
  931. if (cd > GetChineseMonthDays(cy, cm))
  932. {
  933. throw new Exception("不合法的农历日期");
  934. }
  935. offset = offset + cd;
  936. }
  937. //计算月等于闰月
  938. else
  939. {
  940. //如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数
  941. if (this._cIsLeapMonth == true) //计算月为闰月
  942. {
  943. Temp = GetChineseMonthDays(cy, cm); //计算非闰月天数
  944. offset = offset + Temp;
  945. }
  946. if (cd > GetChineseLeapMonthDays(cy))
  947. {
  948. throw new Exception("不合法的农历日期");
  949. }
  950. offset = offset + cd;
  951. }
  952. }
  953. _date = MinDay.AddDays(offset);
  954. }
  955. #endregion
  956. #endregion
  957. #region 私有函数
  958. #region GetChineseMonthDays
  959. /// <summary>
  960. /// //传回农历y年m月的总天数
  961. /// </summary>
  962. private int GetChineseMonthDays(int year, int month)
  963. {
  964. if (BitTest32((LunarDateArray[year - MinYear] & 0x0000FFFF), (16 - month)))
  965. {
  966. return 30;
  967. }
  968. else
  969. {
  970. return 29;
  971. }
  972. }
  973. #endregion
  974. #region GetChineseLeapMonth
  975. /// <summary>
  976. /// 传回农历 y年闰哪个月 1-12 , 没闰传回 0
  977. /// </summary>
  978. private int GetChineseLeapMonth(int year)
  979. {
  980. return LunarDateArray[year - MinYear] & 0xF;
  981. }
  982. #endregion
  983. #region GetChineseLeapMonthDays
  984. /// <summary>
  985. /// 传回农历y年闰月的天数
  986. /// </summary>
  987. private int GetChineseLeapMonthDays(int year)
  988. {
  989. if (GetChineseLeapMonth(year) != 0)
  990. {
  991. if ((LunarDateArray[year - MinYear] & 0x10000) != 0)
  992. {
  993. return 30;
  994. }
  995. else
  996. {
  997. return 29;
  998. }
  999. }
  1000. else
  1001. {
  1002. return 0;
  1003. }
  1004. }
  1005. #endregion
  1006. #region GetChineseYearDays
  1007. /// <summary>
  1008. /// 取农历年一年的天数
  1009. /// </summary>
  1010. private int GetChineseYearDays(int year)
  1011. {
  1012. int i, f, sumDay, info;
  1013. sumDay = 348; //29天*12个月
  1014. i = 0x8000;
  1015. info = LunarDateArray[year - MinYear] & 0x0FFFF;
  1016. //计算12个月中有多少天为30天
  1017. for (int m = 0; m < 12; m++)
  1018. {
  1019. f = info & i;
  1020. if (f != 0)
  1021. {
  1022. sumDay++;
  1023. }
  1024. i = i >> 1;
  1025. }
  1026. return sumDay + GetChineseLeapMonthDays(year);
  1027. }
  1028. #endregion
  1029. #region GetChineseHour
  1030. /// <summary>
  1031. /// 获得当前时间的时辰
  1032. /// </summary>
  1033. private string GetChineseHour(DateTime dt)
  1034. {
  1035. int _hour, _minute, offset, i;
  1036. int indexGan;
  1037. string ganHour, zhiHour;
  1038. string tmpGan;
  1039. //计算时辰的地支
  1040. _hour = dt.Hour; //获得当前时间小时
  1041. _minute = dt.Minute; //获得当前时间分钟
  1042. if (_minute != 0) _hour += 1;
  1043. offset = _hour / 2;
  1044. if (offset >= 12) offset = 0;
  1045. //zhiHour = zhiStr[offset].ToString();
  1046. //计算天干
  1047. TimeSpan ts = this._date - GanZhiStartDay;
  1048. i = ts.Days % 60;
  1049. //ganStr[i % 10] 为日的天干,(n*2-1) %10得出地支对应,n从1开始
  1050. indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1;
  1051. tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(0, indexGan + 2);//凑齐12位
  1052. //ganHour = ganStr[((i % 10 + 1) * 2 - 1) % 10 - 1].ToString();
  1053. return tmpGan[offset].ToString() + zhiStr[offset].ToString();
  1054. }
  1055. #endregion
  1056. #region CheckDateLimit
  1057. /// <summary>
  1058. /// 检查公历日期是否符合要求
  1059. /// </summary>
  1060. private void CheckDateLimit(DateTime dt)
  1061. {
  1062. if ((dt < MinDay) || (dt > MaxDay))
  1063. {
  1064. throw new Exception("超出可转换的日期");
  1065. }
  1066. }
  1067. #endregion
  1068. #region CheckChineseDateLimit
  1069. /// <summary>
  1070. /// 检查农历日期是否合理
  1071. /// </summary>
  1072. private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
  1073. {
  1074. if ((year < MinYear) || (year > MaxYear))
  1075. {
  1076. throw new Exception("非法农历日期");
  1077. }
  1078. if ((month < 1) || (month > 12))
  1079. {
  1080. throw new Exception("非法农历日期");
  1081. }
  1082. if ((day < 1) || (day > 30)) //中国的月最多30天
  1083. {
  1084. throw new Exception("非法农历日期");
  1085. }
  1086. int leap = GetChineseLeapMonth(year);// 计算该年应该闰哪个月
  1087. if ((leapMonth == true) && (month != leap))
  1088. {
  1089. throw new Exception("非法农历日期");
  1090. }
  1091. }
  1092. #endregion
  1093. #region ConvertNumToChineseNum
  1094. /// <summary>
  1095. /// 将0-9转成汉字形式
  1096. /// </summary>
  1097. private string ConvertNumToChineseNum(char n)
  1098. {
  1099. if ((n < '0') || (n > '9')) return "";
  1100. switch (n)
  1101. {
  1102. case '0':
  1103. return HZNum[0].ToString();
  1104. case '1':
  1105. return HZNum[1].ToString();
  1106. case '2':
  1107. return HZNum[2].ToString();
  1108. case '3':
  1109. return HZNum[3].ToString();
  1110. case '4':
  1111. return HZNum[4].ToString();
  1112. case '5':
  1113. return HZNum[5].ToString();
  1114. case '6':
  1115. return HZNum[6].ToString();
  1116. case '7':
  1117. return HZNum[7].ToString();
  1118. case '8':
  1119. return HZNum[8].ToString();
  1120. case '9':
  1121. return HZNum[9].ToString();
  1122. default:
  1123. return "";
  1124. }
  1125. }
  1126. #endregion
  1127. #region BitTest32
  1128. /// <summary>
  1129. /// 测试某位是否为真
  1130. /// </summary>
  1131. private bool BitTest32(int num, int bitpostion)
  1132. {
  1133. if ((bitpostion > 31) || (bitpostion < 0))
  1134. throw new Exception("Error Param: bitpostion[0-31]:" + bitpostion.ToString());
  1135. int bit = 1 << bitpostion;
  1136. if ((num & bit) == 0)
  1137. {
  1138. return false;
  1139. }
  1140. else
  1141. {
  1142. return true;
  1143. }
  1144. }
  1145. #endregion
  1146. #region ConvertDayOfWeek
  1147. /// <summary>
  1148. /// 将星期几转成数字表示
  1149. /// </summary>
  1150. private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
  1151. {
  1152. switch (dayOfWeek)
  1153. {
  1154. case DayOfWeek.Sunday:
  1155. return 1;
  1156. case DayOfWeek.Monday:
  1157. return 2;
  1158. case DayOfWeek.Tuesday:
  1159. return 3;
  1160. case DayOfWeek.Wednesday:
  1161. return 4;
  1162. case DayOfWeek.Thursday:
  1163. return 5;
  1164. case DayOfWeek.Friday:
  1165. return 6;
  1166. case DayOfWeek.Saturday:
  1167. return 7;
  1168. default:
  1169. return 0;
  1170. }
  1171. }
  1172. #endregion
  1173. #region CompareWeekDayHoliday
  1174. /// <summary>
  1175. /// 比较当天是不是指定的第周几
  1176. /// </summary>
  1177. private bool CompareWeekDayHoliday(DateTime date, int month, int week, int day)
  1178. {
  1179. bool ret = false;
  1180. if (date.Month == month) //月份相同
  1181. {
  1182. if (ConvertDayOfWeek(date.DayOfWeek) == day) //星期几相同
  1183. {
  1184. DateTime firstDay = new DateTime(date.Year, date.Month, 1);//生成当月第一天
  1185. int i = ConvertDayOfWeek(firstDay.DayOfWeek);
  1186. int firWeekDays = 7 - ConvertDayOfWeek(firstDay.DayOfWeek) + 1; //计算第一周剩余天数
  1187. if (i > day)
  1188. {
  1189. if ((week - 1) * 7 + day + firWeekDays == date.Day)
  1190. {
  1191. ret = true;
  1192. }
  1193. }
  1194. else
  1195. {
  1196. if (day + firWeekDays + (week - 2) * 7 == date.Day)
  1197. {
  1198. ret = true;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. return ret;
  1204. }
  1205. #endregion
  1206. #endregion
  1207. #region 属性
  1208. #region 节日
  1209. #region newCalendarHoliday
  1210. /// <summary>
  1211. /// 计算中国农历节日
  1212. /// </summary>
  1213. public string newCalendarHoliday
  1214. {
  1215. get
  1216. {
  1217. string tempStr = "";
  1218. if (this._cIsLeapMonth == false) //闰月不计算节日
  1219. {
  1220. foreach (LunarHolidayStruct lh in lHolidayInfo)
  1221. {
  1222. if ((lh.Month == this._cMonth) && (lh.Day == this._cDay))
  1223. {
  1224. tempStr = lh.HolidayName;
  1225. break;
  1226. }
  1227. }
  1228. //对除夕进行特别处理
  1229. if (this._cMonth == 12)
  1230. {
  1231. int i = GetChineseMonthDays(this._cYear, 12); //计算当年农历12月的总天数
  1232. if (this._cDay == i) //如果为最后一天
  1233. {
  1234. tempStr = "除夕";
  1235. }
  1236. }
  1237. }
  1238. return tempStr;
  1239. }
  1240. }
  1241. #endregion
  1242. #region WeekDayHoliday
  1243. /// <summary>
  1244. /// 按某月第几周第几日计算的节日
  1245. /// </summary>
  1246. public string WeekDayHoliday
  1247. {
  1248. get
  1249. {
  1250. string tempStr = "";
  1251. foreach (WeekHolidayStruct wh in wHolidayInfo)
  1252. {
  1253. if (CompareWeekDayHoliday(_date, wh.Month, wh.WeekAtMonth, wh.WeekDay))
  1254. {
  1255. tempStr = wh.HolidayName;
  1256. break;
  1257. }
  1258. }
  1259. return tempStr;
  1260. }
  1261. }
  1262. #endregion
  1263. #region DateHoliday
  1264. /// <summary>
  1265. /// 按公历日计算的节日
  1266. /// </summary>
  1267. public string DateHoliday
  1268. {
  1269. get
  1270. {
  1271. string tempStr = "";
  1272. foreach (SolarHolidayStruct sh in sHolidayInfo)
  1273. {
  1274. if ((sh.Month == _date.Month) && (sh.Day == _date.Day))
  1275. {
  1276. tempStr = sh.HolidayName;
  1277. break;
  1278. }
  1279. }
  1280. return tempStr;
  1281. }
  1282. }
  1283. #endregion
  1284. #endregion
  1285. #region 公历日期
  1286. #region Date
  1287. /// <summary>
  1288. /// 取对应的公历日期
  1289. /// </summary>
  1290. public DateTime Date
  1291. {
  1292. get { return _date; }
  1293. set { _date = value; }
  1294. }
  1295. #endregion
  1296. #region WeekDay
  1297. /// <summary>
  1298. /// 取星期几
  1299. /// </summary>
  1300. public DayOfWeek WeekDay
  1301. {
  1302. get { return _date.DayOfWeek; }
  1303. }
  1304. #endregion
  1305. #region WeekDayStr
  1306. /// <summary>
  1307. /// 周几的字符
  1308. /// </summary>
  1309. public string WeekDayStr
  1310. {
  1311. get
  1312. {
  1313. switch (_date.DayOfWeek)
  1314. {
  1315. case DayOfWeek.Sunday:
  1316. return "星期日";
  1317. case DayOfWeek.Monday:
  1318. return "星期一";
  1319. case DayOfWeek.Tuesday:
  1320. return "星期二";
  1321. case DayOfWeek.Wednesday:
  1322. return "星期三";
  1323. case DayOfWeek.Thursday:
  1324. return "星期四";
  1325. case DayOfWeek.Friday:
  1326. return "星期五";
  1327. default:
  1328. return "星期六";
  1329. }
  1330. }
  1331. }
  1332. #endregion
  1333. #region DateString
  1334. /// <summary>
  1335. /// 公历日期中文表示法 如一九九七年七月一日
  1336. /// </summary>
  1337. public string DateString
  1338. {
  1339. get
  1340. {
  1341. return "公元" + this._date.ToLongDateString();
  1342. }
  1343. }
  1344. #endregion
  1345. #region IsLeapYear
  1346. /// <summary>
  1347. /// 当前是否公历闰年
  1348. /// </summary>
  1349. public bool IsLeapYear
  1350. {
  1351. get
  1352. {
  1353. return DateTime.IsLeapYear(this._date.Year);
  1354. }
  1355. }
  1356. #endregion
  1357. #region ChineseConstellation
  1358. /// <summary>
  1359. /// 28星宿计算
  1360. /// </summary>
  1361. public string ChineseConstellation
  1362. {
  1363. get
  1364. {
  1365. int offset = 0;
  1366. int modStarDay = 0;
  1367. TimeSpan ts = this._date - ChineseConstellationReferDay;
  1368. offset = ts.Days;
  1369. modStarDay = offset % 28;
  1370. return (modStarDay >= 0 ? _chineseConstellationName[modStarDay] : _chineseConstellationName[27 + modStarDay]);
  1371. }
  1372. }
  1373. #endregion
  1374. #region ChineseHour
  1375. /// <summary>
  1376. /// 时辰
  1377. /// </summary>
  1378. public string ChineseHour
  1379. {
  1380. get
  1381. {
  1382. return GetChineseHour(_datetime);
  1383. }
  1384. }
  1385. #endregion
  1386. #endregion
  1387. #region 农历日期
  1388. #region IsChineseLeapMonth
  1389. /// <summary>
  1390. /// 是否闰月
  1391. /// </summary>
  1392. public bool IsChineseLeapMonth
  1393. {
  1394. get { return this._cIsLeapMonth; }
  1395. }
  1396. #endregion
  1397. #region IsChineseLeapYear
  1398. /// <summary>
  1399. /// 当年是否有闰月
  1400. /// </summary>
  1401. public bool IsChineseLeapYear
  1402. {
  1403. get
  1404. {
  1405. return this._cIsLeapYear;
  1406. }
  1407. }
  1408. #endregion
  1409. #region ChineseDay
  1410. /// <summary>
  1411. /// 农历日
  1412. /// </summary>
  1413. public int ChineseDay
  1414. {
  1415. get { return this._cDay; }
  1416. }
  1417. #endregion
  1418. #region ChineseDayString
  1419. /// <summary>
  1420. /// 农历日中文表示
  1421. /// </summary>
  1422. public string ChineseDayString
  1423. {
  1424. get
  1425. {
  1426. switch (this._cDay)
  1427. {
  1428. case 0:
  1429. return "";
  1430. case 10:
  1431. return "初十";
  1432. case 20:
  1433. return "二十";
  1434. case 30:
  1435. return "三十";
  1436. default:
  1437. return nStr2[(int)(_cDay / 10)].ToString() + nStr1[_cDay % 10].ToString();
  1438. }
  1439. }
  1440. }
  1441. #endregion
  1442. #region ChineseMonth
  1443. /// <summary>
  1444. /// 农历的月份
  1445. /// </summary>
  1446. public int ChineseMonth
  1447. {
  1448. get { return this._cMonth; }
  1449. }
  1450. #endregion
  1451. #region ChineseMonthString
  1452. /// <summary>
  1453. /// 农历月份字符串
  1454. /// </summary>
  1455. public string ChineseMonthString
  1456. {
  1457. get
  1458. {
  1459. return _monthString[this._cMonth];
  1460. }
  1461. }
  1462. #endregion
  1463. #region ChineseYear
  1464. /// <summary>
  1465. /// 取农历年份
  1466. /// </summary>
  1467. public int ChineseYear
  1468. {
  1469. get { return this._cYear; }
  1470. }
  1471. #endregion
  1472. #region ChineseYearString
  1473. /// <summary>
  1474. /// 取农历年字符串如,一九九七年
  1475. /// </summary>
  1476. public string ChineseYearString
  1477. {
  1478. get
  1479. {
  1480. string tempStr = "";
  1481. string num = this._cYear.ToString();
  1482. for (int i = 0; i < 4; i++)
  1483. {
  1484. tempStr += ConvertNumToChineseNum(num[i]);
  1485. }
  1486. return tempStr + "年";
  1487. }
  1488. }
  1489. #endregion
  1490. #region ChineseDateString
  1491. /// <summary>
  1492. /// 取农历日期表示法:农历一九九七年正月初五
  1493. /// </summary>
  1494. public string ChineseDateString
  1495. {
  1496. get
  1497. {
  1498. if (this._cIsLeapMonth == true)
  1499. {
  1500. return "农历" + ChineseYearString + "闰" + ChineseMonthString + ChineseDayString;
  1501. }
  1502. else
  1503. {
  1504. return "农历" + ChineseYearString + ChineseMonthString + ChineseDayString;
  1505. }
  1506. }
  1507. }
  1508. #endregion
  1509. #region ChineseTwentyFourDay
  1510. /// <summary>
  1511. /// 定气法计算二十四节气,二十四节气是按地球公转来计算的,并非是阴历计算的
  1512. /// </summary>
  1513. /// <remarks>
  1514. /// 节气的定法有两种。古代历法采用的称为"恒气",即按时间把一年等分为24份,
  1515. /// 每一节气平均得15天有余,所以又称"平气"。现代农历采用的称为"定气",即
  1516. /// 按地球在轨道上的位置为标准,一周360°,两节气之间相隔15°。由于冬至时地
  1517. /// 球位于近日点附近,运动速度较快,因而太阳在黄道上移动15°的时间不到15天。
  1518. /// 夏至前后的情况正好相反,太阳在黄道上移动较慢,一个节气达16天之多。采用
  1519. /// 定气时可以保证春、秋两分必然在昼夜平分的那两天。
  1520. /// </remarks>
  1521. public string ChineseTwentyFourDay
  1522. {
  1523. get
  1524. {
  1525. DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1526. DateTime newDate;
  1527. double num;
  1528. int y;
  1529. string tempStr = "";
  1530. y = this._date.Year;
  1531. for (int i = 1; i <= 24; i++)
  1532. {
  1533. num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
  1534. newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
  1535. if (newDate.DayOfYear == _date.DayOfYear)
  1536. {
  1537. tempStr = SolarTerm[i - 1];
  1538. break;
  1539. }
  1540. }
  1541. return tempStr;
  1542. }
  1543. }
  1544. //当前日期前一个最近节气
  1545. public string ChineseTwentyFourPrevDay
  1546. {
  1547. get
  1548. {
  1549. DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1550. DateTime newDate;
  1551. double num;
  1552. int y;
  1553. string tempStr = "";
  1554. y = this._date.Year;
  1555. for (int i = 24; i >= 1; i--)
  1556. {
  1557. num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
  1558. newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
  1559. if (newDate.DayOfYear < _date.DayOfYear)
  1560. {
  1561. tempStr = string.Format("{0}[小贝]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"));
  1562. break;
  1563. }
  1564. }
  1565. return tempStr;
  1566. }
  1567. }
  1568. //当前日期后一个最近节气
  1569. public string ChineseTwentyFourNextDay
  1570. {
  1571. get
  1572. {
  1573. DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1574. DateTime newDate;
  1575. double num;
  1576. int y;
  1577. string tempStr = "";
  1578. y = this._date.Year;
  1579. for (int i = 1; i <= 24; i++)
  1580. {
  1581. num = 525948.76 * (y - 1900) + sTermInfo[i - 1];
  1582. newDate = baseDateAndTime.AddMinutes(num);//按分钟计算
  1583. if (newDate.DayOfYear > _date.DayOfYear)
  1584. {
  1585. tempStr = string.Format("{0}[小贝]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"));
  1586. break;
  1587. }
  1588. }
  1589. return tempStr;
  1590. }
  1591. }
  1592. #endregion
  1593. #endregion
  1594. #region 星座
  1595. /// <summary>
  1596. /// 计算指定日期的星座序号
  1597. /// </summary>
  1598. public string Constellation
  1599. {
  1600. get
  1601. {
  1602. int index = 0;
  1603. int y, m, d;
  1604. y = _date.Year;
  1605. m = _date.Month;
  1606. d = _date.Day;
  1607. y = m * 100 + d;
  1608. if (((y >= 321) && (y <= 419))) { index = 0; }
  1609. else if ((y >= 420) && (y <= 520)) { index = 1; }
  1610. else if ((y >= 521) && (y <= 620)) { index = 2; }
  1611. else if ((y >= 621) && (y <= 722)) { index = 3; }
  1612. else if ((y >= 723) && (y <= 822)) { index = 4; }
  1613. else if ((y >= 823) && (y <= 922)) { index = 5; }
  1614. else if ((y >= 923) && (y <= 1022)) { index = 6; }
  1615. else if ((y >= 1023) && (y <= 1121)) { index = 7; }
  1616. else if ((y >= 1122) && (y <= 1221)) { index = 8; }
  1617. else if ((y >= 1222) || (y <= 119)) { index = 9; }
  1618. else if ((y >= 120) && (y <= 218)) { index = 10; }
  1619. else if ((y >= 219) && (y <= 320)) { index = 11; }
  1620. else { index = 0; }
  1621. return _constellationName[index];
  1622. }
  1623. }
  1624. #endregion
  1625. #region 属相
  1626. #region Animal
  1627. /// <summary>
  1628. /// 计算属相的索引,注意虽然属相是以农历年来区别的,但是目前在实际使用中是按公历来计算的
  1629. /// 鼠年为1,其它类推
  1630. /// </summary>
  1631. public int Animal
  1632. {
  1633. get
  1634. {
  1635. int offset = _date.Year - AnimalStartYear;
  1636. return (offset % 12) + 1;
  1637. }
  1638. }
  1639. #endregion
  1640. #region AnimalString
  1641. /// <summary>
  1642. /// 取属相字符串
  1643. /// </summary>
  1644. public string AnimalString
  1645. {
  1646. get
  1647. {
  1648. int offset = _date.Year - AnimalStartYear; //阳历计算
  1649. //int offset = this._cYear - AnimalStartYear; 农历计算
  1650. return animalStr[offset % 12].ToString();
  1651. }
  1652. }
  1653. #endregion
  1654. #endregion
  1655. #region 天干地支
  1656. #region GanZhiYearString
  1657. /// <summary>
  1658. /// 取农历年的干支表示法如 乙丑年
  1659. /// </summary>
  1660. public string GanZhiYearString
  1661. {
  1662. get
  1663. {
  1664. string tempStr;
  1665. int i = (this._cYear - GanZhiStartYear) % 60; //计算干支
  1666. tempStr = ganStr[i % 10].ToString() + zhiStr[i % 12].ToString() + "年";
  1667. return tempStr;
  1668. }
  1669. }
  1670. #endregion
  1671. #region GanZhiMonthString
  1672. /// <summary>
  1673. /// 取干支的月表示字符串,注意农历的闰月不记干支
  1674. /// </summary>
  1675. public string GanZhiMonthString
  1676. {
  1677. get
  1678. {
  1679. //每个月的地支总是固定的,而且总是从寅月开始
  1680. int zhiIndex;
  1681. string zhi;
  1682. if (this._cMonth > 10)
  1683. {
  1684. zhiIndex = this._cMonth - 10;
  1685. }
  1686. else
  1687. {
  1688. zhiIndex = this._cMonth + 2;
  1689. }
  1690. zhi = zhiStr[zhiIndex - 1].ToString();
  1691. //根据当年的干支年的干来计算月干的第一个
  1692. int ganIndex = 1;
  1693. string gan;
  1694. int i = (this._cYear - GanZhiStartYear) % 60; //计算干支
  1695. switch (i % 10)
  1696. {
  1697. #region ...
  1698. case 0: //甲
  1699. ganIndex = 3;
  1700. break;
  1701. case 1: //乙
  1702. ganIndex = 5;
  1703. break;
  1704. case 2: //丙
  1705. ganIndex = 7;
  1706. break;
  1707. case 3: //丁
  1708. ganIndex = 9;
  1709. break;
  1710. case 4: //戊
  1711. ganIndex = 1;
  1712. break;
  1713. case 5: //己
  1714. ganIndex = 3;
  1715. break;
  1716. case 6: //庚
  1717. ganIndex = 5;
  1718. break;
  1719. case 7: //辛
  1720. ganIndex = 7;
  1721. break;
  1722. case 8: //壬
  1723. ganIndex = 9;
  1724. break;
  1725. case 9: //癸
  1726. ganIndex = 1;
  1727. break;
  1728. #endregion
  1729. }
  1730. gan = ganStr[(ganIndex + this._cMonth - 2) % 10].ToString();
  1731. return gan + zhi + "月";
  1732. }
  1733. }
  1734. #endregion
  1735. #region GanZhiDayString
  1736. /// <summary>
  1737. /// 取干支日表示法
  1738. /// </summary>
  1739. public string GanZhiDayString
  1740. {
  1741. get
  1742. {
  1743. int i, offset;
  1744. TimeSpan ts = this._date - GanZhiStartDay;
  1745. offset = ts.Days;
  1746. i = offset % 60;
  1747. return ganStr[i % 10].ToString() + zhiStr[i % 12].ToString() + "日";
  1748. }
  1749. }
  1750. #endregion
  1751. #region GanZhiDateString
  1752. /// <summary>
  1753. /// 取当前日期的干支表示法如 甲子年乙丑月丙庚日
  1754. /// </summary>
  1755. public string GanZhiDateString
  1756. {
  1757. get
  1758. {
  1759. return GanZhiYearString + GanZhiMonthString + GanZhiDayString;
  1760. }
  1761. }
  1762. #endregion
  1763. #endregion
  1764. #endregion
  1765. }
  1766. }
复制代码

效果格式如下:

以上所述是小编给大家介绍的C# 日历类功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!

您可能感兴趣的文章:

  • C# 常用日期时间函数(老用不熟)
  • c#实现输出本月的月历
  • C#由当前日期计算相应的周一和周日的实例代码
  • c#的时间日期操作示例分享(c#获取当前日期)
  • C#实现带阴历显示的日期代码
  • C#获取上个月第一天和最后一天日期的方法
  • C#实现农历日历的方法
  • C#日期控件datetimepicker保存空值的三种方法
  • C#日历样式的下拉式计算器实例讲解
  • C#简单输出日历的方法
  • C#实现的阴历阳历互相转化类实例


回复

使用道具 举报