ChineseCalendar.cs 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Masuit.Tools.DateTimeExt
  5. {
  6. /// <summary>
  7. /// 中国农历类 支持 1900.1.31日起至 2069.12.31日止的数据
  8. /// </summary>
  9. /// <remarks>
  10. /// 本程序使用数据来源于网上的万年历查询,并综合了一些其它数据
  11. /// </remarks>
  12. public class ChineseCalendar
  13. {
  14. private class ChineseCalendarException : Exception
  15. {
  16. public ChineseCalendarException(string msg) : base(msg)
  17. {
  18. }
  19. }
  20. private readonly DateTime _datetime;
  21. #region 基础数据
  22. private const int MinYear = 1900;
  23. private const int MaxYear = 2050;
  24. private static readonly DateTime MinDay = new DateTime(1900, 1, 30);
  25. private static readonly DateTime MaxDay = new DateTime(2049, 12, 31);
  26. private const int GanZhiStartYear = 1864; //干支计算起始年
  27. private static readonly DateTime GanZhiStartDay = new DateTime(1899, 12, 22); //起始日
  28. private const string HzNum = "零一二三四五六七八九";
  29. private const int AnimalStartYear = 1900; //1900年为鼠年
  30. private static readonly DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13); //28星宿参考值,本日为角
  31. /// <summary>
  32. /// 来源于网上的农历数据
  33. /// </summary>
  34. /// <remarks>
  35. /// 数据结构如下,共使用17位数据
  36. /// 第17位:表示闰月天数,0表示29天 1表示30天
  37. /// 第16位-第5位(共12位)表示12个月,其中第16位表示第一月,如果该月为30天则为1,29天为0
  38. /// 第4位-第1位(共4位)表示闰月是哪个月,如果当年没有闰月,则置0
  39. ///</remarks>
  40. private static readonly int[] LunarDateArray =
  41. {
  42. 0x04BD8,
  43. 0x04AE0,
  44. 0x0A570,
  45. 0x054D5,
  46. 0x0D260,
  47. 0x0D950,
  48. 0x16554,
  49. 0x056A0,
  50. 0x09AD0,
  51. 0x055D2,
  52. 0x04AE0,
  53. 0x0A5B6,
  54. 0x0A4D0,
  55. 0x0D250,
  56. 0x1D255,
  57. 0x0B540,
  58. 0x0D6A0,
  59. 0x0ADA2,
  60. 0x095B0,
  61. 0x14977,
  62. 0x04970,
  63. 0x0A4B0,
  64. 0x0B4B5,
  65. 0x06A50,
  66. 0x06D40,
  67. 0x1AB54,
  68. 0x02B60,
  69. 0x09570,
  70. 0x052F2,
  71. 0x04970,
  72. 0x06566,
  73. 0x0D4A0,
  74. 0x0EA50,
  75. 0x06E95,
  76. 0x05AD0,
  77. 0x02B60,
  78. 0x186E3,
  79. 0x092E0,
  80. 0x1C8D7,
  81. 0x0C950,
  82. 0x0D4A0,
  83. 0x1D8A6,
  84. 0x0B550,
  85. 0x056A0,
  86. 0x1A5B4,
  87. 0x025D0,
  88. 0x092D0,
  89. 0x0D2B2,
  90. 0x0A950,
  91. 0x0B557,
  92. 0x06CA0,
  93. 0x0B550,
  94. 0x15355,
  95. 0x04DA0,
  96. 0x0A5B0,
  97. 0x14573,
  98. 0x052B0,
  99. 0x0A9A8,
  100. 0x0E950,
  101. 0x06AA0,
  102. 0x0AEA6,
  103. 0x0AB50,
  104. 0x04B60,
  105. 0x0AAE4,
  106. 0x0A570,
  107. 0x05260,
  108. 0x0F263,
  109. 0x0D950,
  110. 0x05B57,
  111. 0x056A0,
  112. 0x096D0,
  113. 0x04DD5,
  114. 0x04AD0,
  115. 0x0A4D0,
  116. 0x0D4D4,
  117. 0x0D250,
  118. 0x0D558,
  119. 0x0B540,
  120. 0x0B6A0,
  121. 0x195A6,
  122. 0x095B0,
  123. 0x049B0,
  124. 0x0A974,
  125. 0x0A4B0,
  126. 0x0B27A,
  127. 0x06A50,
  128. 0x06D40,
  129. 0x0AF46,
  130. 0x0AB60,
  131. 0x09570,
  132. 0x04AF5,
  133. 0x04970,
  134. 0x064B0,
  135. 0x074A3,
  136. 0x0EA50,
  137. 0x06B58,
  138. 0x055C0,
  139. 0x0AB60,
  140. 0x096D5,
  141. 0x092E0,
  142. 0x0C960,
  143. 0x0D954,
  144. 0x0D4A0,
  145. 0x0DA50,
  146. 0x07552,
  147. 0x056A0,
  148. 0x0ABB7,
  149. 0x025D0,
  150. 0x092D0,
  151. 0x0CAB5,
  152. 0x0A950,
  153. 0x0B4A0,
  154. 0x0BAA4,
  155. 0x0AD50,
  156. 0x055D9,
  157. 0x04BA0,
  158. 0x0A5B0,
  159. 0x15176,
  160. 0x052B0,
  161. 0x0A930,
  162. 0x07954,
  163. 0x06AA0,
  164. 0x0AD50,
  165. 0x05B52,
  166. 0x04B60,
  167. 0x0A6E6,
  168. 0x0A4E0,
  169. 0x0D260,
  170. 0x0EA65,
  171. 0x0D530,
  172. 0x05AA0,
  173. 0x076A3,
  174. 0x096D0,
  175. 0x04BD7,
  176. 0x04AD0,
  177. 0x0A4D0,
  178. 0x1D0B6,
  179. 0x0D250,
  180. 0x0D520,
  181. 0x0DD45,
  182. 0x0B5A0,
  183. 0x056D0,
  184. 0x055B2,
  185. 0x049B0,
  186. 0x0A577,
  187. 0x0A4B0,
  188. 0x0AA50,
  189. 0x1B255,
  190. 0x06D20,
  191. 0x0ADA0,
  192. 0x14B63
  193. };
  194. /// <summary>
  195. /// 星座
  196. /// </summary>
  197. private static readonly string[] ConstellationName =
  198. {
  199. "白羊座",
  200. "金牛座",
  201. "双子座",
  202. "巨蟹座",
  203. "狮子座",
  204. "处女座",
  205. "天秤座",
  206. "天蝎座",
  207. "射手座",
  208. "摩羯座",
  209. "水瓶座",
  210. "双鱼座"
  211. };
  212. /// <summary>
  213. /// 二十四节气
  214. /// </summary>
  215. private static readonly string[] LunarHolidayName =
  216. {
  217. "小寒",
  218. "大寒",
  219. "立春",
  220. "雨水",
  221. "惊蛰",
  222. "春分",
  223. "清明",
  224. "谷雨",
  225. "立夏",
  226. "小满",
  227. "芒种",
  228. "夏至",
  229. "小暑",
  230. "大暑",
  231. "立秋",
  232. "处暑",
  233. "白露",
  234. "秋分",
  235. "寒露",
  236. "霜降",
  237. "立冬",
  238. "小雪",
  239. "大雪",
  240. "冬至"
  241. };
  242. /// <summary>
  243. /// 二十八星宿
  244. /// </summary>
  245. private static readonly string[] ChineseConstellationName =
  246. {
  247. //四 五 六 日 一 二 三
  248. "角木蛟",
  249. "亢金龙",
  250. "女土蝠",
  251. "房日兔",
  252. "心月狐",
  253. "尾火虎",
  254. "箕水豹",
  255. "斗木獬",
  256. "牛金牛",
  257. "氐土貉",
  258. "虚日鼠",
  259. "危月燕",
  260. "室火猪",
  261. "壁水獝",
  262. "奎木狼",
  263. "娄金狗",
  264. "胃土彘",
  265. "昴日鸡",
  266. "毕月乌",
  267. "觜火猴",
  268. "参水猿",
  269. "井木犴",
  270. "鬼金羊",
  271. "柳土獐",
  272. "星日马",
  273. "张月鹿",
  274. "翼火蛇",
  275. "轸水蚓"
  276. };
  277. #region 节气数据
  278. /// <summary>
  279. /// 节气数据
  280. /// </summary>
  281. private static readonly string[] SolarTerm =
  282. {
  283. "小寒",
  284. "大寒",
  285. "立春",
  286. "雨水",
  287. "惊蛰",
  288. "春分",
  289. "清明",
  290. "谷雨",
  291. "立夏",
  292. "小满",
  293. "芒种",
  294. "夏至",
  295. "小暑",
  296. "大暑",
  297. "立秋",
  298. "处暑",
  299. "白露",
  300. "秋分",
  301. "寒露",
  302. "霜降",
  303. "立冬",
  304. "小雪",
  305. "大雪",
  306. "冬至"
  307. };
  308. private static readonly int[] STermInfo =
  309. {
  310. 0,
  311. 21208,
  312. 42467,
  313. 63836,
  314. 85337,
  315. 107014,
  316. 128867,
  317. 150921,
  318. 173149,
  319. 195551,
  320. 218072,
  321. 240693,
  322. 263343,
  323. 285989,
  324. 308563,
  325. 331033,
  326. 353350,
  327. 375494,
  328. 397447,
  329. 419210,
  330. 440795,
  331. 462224,
  332. 483532,
  333. 504758
  334. };
  335. #endregion
  336. #region 农历相关数据
  337. private const string TianGan = "甲乙丙丁戊己庚辛壬癸";
  338. private const string DiZhi = "子丑寅卯辰巳午未申酉戌亥";
  339. private const string AnimalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
  340. private const string NStr1 = "日一二三四五六七八九";
  341. private const string NStr2 = "初十廿卅";
  342. private static readonly string[] MonthString =
  343. {
  344. "出错",
  345. "正月",
  346. "二月",
  347. "三月",
  348. "四月",
  349. "五月",
  350. "六月",
  351. "七月",
  352. "八月",
  353. "九月",
  354. "十月",
  355. "十一月",
  356. "腊月"
  357. };
  358. #endregion
  359. #region 节日数据
  360. /// <summary>
  361. /// 自定义的工作日
  362. /// </summary>
  363. public static HashSet<DateTime> CustomWorkDays { get; } = new HashSet<DateTime>();
  364. /// <summary>
  365. /// 自定义的节假日
  366. /// </summary>
  367. public static Dictionary<DateTime, string> CustomHolidays { get; } = new Dictionary<DateTime, string>();
  368. /// <summary>
  369. /// 按公历计算的通用节假日
  370. /// </summary>
  371. private static HashSet<DateInfoStruct> SolarHolidayInfo { get; } = new HashSet<DateInfoStruct>
  372. {
  373. new DateInfoStruct(1, 1, 1, "元旦"),
  374. new DateInfoStruct(2, 2, 0, "世界湿地日"),
  375. new DateInfoStruct(2, 10, 0, "国际气象节"),
  376. new DateInfoStruct(2, 14, 0, "情人节"),
  377. new DateInfoStruct(3, 1, 0, "国际海豹日"),
  378. new DateInfoStruct(3, 5, 0, "学雷锋纪念日"),
  379. new DateInfoStruct(3, 8, 0, "妇女节"),
  380. new DateInfoStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),
  381. new DateInfoStruct(3, 14, 0, "国际警察日"),
  382. new DateInfoStruct(3, 15, 0, "消费者权益日"),
  383. new DateInfoStruct(3, 17, 0, "中国国医节 国际航海日"),
  384. new DateInfoStruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日"),
  385. new DateInfoStruct(3, 22, 0, "世界水日"),
  386. new DateInfoStruct(3, 24, 0, "世界防治结核病日"),
  387. new DateInfoStruct(4, 1, 0, "愚人节"),
  388. new DateInfoStruct(4, 5, 1, "清明节"),
  389. new DateInfoStruct(4, 7, 0, "世界卫生日"),
  390. new DateInfoStruct(4, 22, 0, "世界地球日"),
  391. new DateInfoStruct(5, 1, 1, "劳动节"),
  392. new DateInfoStruct(5, 4, 0, "青年节"),
  393. new DateInfoStruct(5, 8, 0, "世界红十字日"),
  394. new DateInfoStruct(5, 12, 0, "国际护士节"),
  395. new DateInfoStruct(5, 31, 0, "世界无烟日"),
  396. new DateInfoStruct(6, 1, 0, "国际儿童节"),
  397. new DateInfoStruct(6, 5, 0, "世界环境保护日"),
  398. new DateInfoStruct(6, 26, 0, "国际禁毒日"),
  399. new DateInfoStruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日"),
  400. new DateInfoStruct(7, 11, 0, "世界人口日"),
  401. new DateInfoStruct(8, 1, 0, "建军节"),
  402. new DateInfoStruct(8, 8, 0, "中国男子节 父亲节"),
  403. new DateInfoStruct(8, 15, 0, "抗日战争胜利纪念"),
  404. new DateInfoStruct(9, 9, 0, " 逝世纪念"),
  405. new DateInfoStruct(9, 10, 0, "教师节"),
  406. new DateInfoStruct(9, 18, 0, "九·一八事变纪念日"),
  407. new DateInfoStruct(9, 20, 0, "国际爱牙日"),
  408. new DateInfoStruct(9, 27, 0, "世界旅游日"),
  409. new DateInfoStruct(9, 28, 0, "孔子诞辰"),
  410. new DateInfoStruct(10, 1, 7, "国庆节 国际音乐日"),
  411. new DateInfoStruct(10, 24, 0, "联合国日"),
  412. new DateInfoStruct(11, 10, 0, "世界青年节"),
  413. new DateInfoStruct(11, 12, 0, "孙中山诞辰纪念"),
  414. new DateInfoStruct(12, 1, 0, "世界艾滋病日"),
  415. new DateInfoStruct(12, 3, 0, "世界残疾人日"),
  416. new DateInfoStruct(12, 20, 0, "澳门回归纪念"),
  417. new DateInfoStruct(12, 24, 0, "平安夜"),
  418. new DateInfoStruct(12, 25, 0, "圣诞节"),
  419. new DateInfoStruct(12, 26, 0, " 诞辰纪念")
  420. };
  421. /// <summary>
  422. /// 按农历计算的通用节假日
  423. /// </summary>
  424. private static HashSet<DateInfoStruct> LunarHolidayInfo { get; } = new HashSet<DateInfoStruct>
  425. {
  426. new DateInfoStruct(1, 1, 6, "春节"),
  427. new DateInfoStruct(1, 15, 0, "元宵节"),
  428. new DateInfoStruct(5, 5, 1, "端午节"),
  429. new DateInfoStruct(7, 7, 0, "七夕情人节"),
  430. new DateInfoStruct(7, 15, 0, "中元节 盂兰盆节"),
  431. new DateInfoStruct(8, 15, 1, "中秋节"),
  432. new DateInfoStruct(9, 9, 0, "重阳节"),
  433. new DateInfoStruct(12, 8, 0, "腊八节"),
  434. new DateInfoStruct(12, 23, 0, "北方小年(扫房)"),
  435. new DateInfoStruct(12, 24, 0, "南方小年(掸尘)"),
  436. //new HolidayStruct(12, 30, 0, "除夕") //注意除夕需要其它方法进行计算
  437. };
  438. private static readonly WeekHolidayStruct[] WHolidayInfo =
  439. {
  440. new WeekHolidayStruct(5, 2, 1, "母亲节"),
  441. new WeekHolidayStruct(5, 3, 1, "全国助残日"),
  442. new WeekHolidayStruct(6, 3, 1, "父亲节"),
  443. new WeekHolidayStruct(9, 3, 3, "国际和平日"),
  444. new WeekHolidayStruct(9, 4, 1, "国际聋人节"),
  445. new WeekHolidayStruct(10, 1, 2, "国际住房日"),
  446. new WeekHolidayStruct(10, 1, 4, "国际减轻自然灾害日"),
  447. new WeekHolidayStruct(11, 4, 5, "感恩节")
  448. };
  449. #endregion
  450. #endregion
  451. /// <summary>
  452. /// 用一个标准的公历日期来初使化
  453. /// </summary>
  454. /// <param name="dt"></param>
  455. public ChineseCalendar(DateTime dt)
  456. {
  457. int i;
  458. Date = dt.Date;
  459. _datetime = dt;
  460. //农历日期计算部分
  461. var temp = 0;
  462. var ts = Date - MinDay; //计算两天的基本差距
  463. var offset = ts.Days;
  464. for (i = MinYear; i <= MaxYear; i++)
  465. {
  466. temp = GetChineseYearDays(i); //求当年农历年天数
  467. if (offset - temp < 1)
  468. {
  469. break;
  470. }
  471. offset = offset - temp;
  472. }
  473. ChineseYear = i;
  474. var leap = GetChineseLeapMonth(ChineseYear);
  475. //设定当年是否有闰月
  476. IsChineseLeapYear = leap > 0;
  477. IsChineseLeapMonth = false;
  478. for (i = 1; i <= 12; i++)
  479. {
  480. //闰月
  481. if (leap > 0 && i == leap + 1 && IsChineseLeapMonth == false)
  482. {
  483. IsChineseLeapMonth = true;
  484. i = i - 1;
  485. temp = GetChineseLeapMonthDays(ChineseYear); //计算闰月天数
  486. }
  487. else
  488. {
  489. IsChineseLeapMonth = false;
  490. temp = GetChineseMonthDays(ChineseYear, i); //计算非闰月天数
  491. }
  492. offset = offset - temp;
  493. if (offset <= 0)
  494. {
  495. break;
  496. }
  497. }
  498. offset = offset + temp;
  499. ChineseMonth = i;
  500. ChineseDay = offset;
  501. }
  502. /// <summary>
  503. /// 用农历的日期来初使化
  504. /// </summary>
  505. /// <param name="cy">农历年</param>
  506. /// <param name="cm">农历月</param>
  507. /// <param name="cd">农历日</param>
  508. /// <param name="leapMonthFlag">闰月标志</param>
  509. public ChineseCalendar(int cy, int cm, int cd, bool leapMonthFlag)
  510. {
  511. int i, temp;
  512. CheckChineseDateLimit(cy, cm, cd, leapMonthFlag);
  513. ChineseYear = cy;
  514. ChineseMonth = cm;
  515. ChineseDay = cd;
  516. var offset = 0;
  517. for (i = MinYear; i < cy; i++)
  518. {
  519. temp = GetChineseYearDays(i); //求当年农历年天数
  520. offset = offset + temp;
  521. }
  522. var leap = GetChineseLeapMonth(cy);
  523. IsChineseLeapYear = leap != 0;
  524. IsChineseLeapMonth = cm == leap && leapMonthFlag;
  525. if (IsChineseLeapYear == false || cm < leap) //当年没有闰月||计算月份小于闰月
  526. {
  527. for (i = 1; i < cm; i++)
  528. {
  529. temp = GetChineseMonthDays(cy, i); //计算非闰月天数
  530. offset = offset + temp;
  531. }
  532. //检查日期是否大于最大天
  533. if (cd > GetChineseMonthDays(cy, cm))
  534. {
  535. throw new ChineseCalendarException("不合法的农历日期");
  536. }
  537. offset = offset + cd; //加上当月的天数
  538. }
  539. else //是闰年,且计算月份大于或等于闰月
  540. {
  541. for (i = 1; i < cm; i++)
  542. {
  543. temp = GetChineseMonthDays(cy, i); //计算非闰月天数
  544. offset = offset + temp;
  545. }
  546. if (cm > leap) //计算月大于闰月
  547. {
  548. temp = GetChineseLeapMonthDays(cy); //计算闰月天数
  549. offset = offset + temp; //加上闰月天数
  550. if (cd > GetChineseMonthDays(cy, cm))
  551. {
  552. throw new ChineseCalendarException("不合法的农历日期");
  553. }
  554. offset = offset + cd;
  555. }
  556. else //计算月等于闰月
  557. {
  558. //如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数
  559. if (IsChineseLeapMonth) //计算月为闰月
  560. {
  561. temp = GetChineseMonthDays(cy, cm); //计算非闰月天数
  562. offset = offset + temp;
  563. }
  564. if (cd > GetChineseLeapMonthDays(cy))
  565. {
  566. throw new ChineseCalendarException("不合法的农历日期");
  567. }
  568. offset = offset + cd;
  569. }
  570. }
  571. Date = MinDay.AddDays(offset);
  572. }
  573. #region 私有函数
  574. /// <summary>
  575. /// 传回农历 y年m月的总天数
  576. /// </summary>
  577. /// <param name="year"></param>
  578. /// <param name="month"></param>
  579. /// <returns></returns>
  580. private int GetChineseMonthDays(int year, int month)
  581. {
  582. return BitTest32(LunarDateArray[year - MinYear] & 0x0000FFFF, 16 - month) ? 30 : 29;
  583. }
  584. /// <summary>
  585. /// 传回农历 y年闰哪个月 1-12 , 没闰传回 0
  586. /// </summary>
  587. /// <param name="year"></param>
  588. /// <returns></returns>
  589. private int GetChineseLeapMonth(int year)
  590. {
  591. return LunarDateArray[year - MinYear] & 0xF;
  592. }
  593. /// <summary>
  594. /// 传回农历 y年闰月的天数
  595. /// </summary>
  596. /// <param name="year"></param>
  597. /// <returns></returns>
  598. private int GetChineseLeapMonthDays(int year)
  599. {
  600. return GetChineseLeapMonth(year) != 0 ? (LunarDateArray[year - MinYear] & 0x10000) != 0 ? 30 : 29 : 0;
  601. }
  602. /// <summary>
  603. /// 取农历年一年的天数
  604. /// </summary>
  605. /// <param name="year"></param>
  606. /// <returns></returns>
  607. private int GetChineseYearDays(int year)
  608. {
  609. var sumDay = 348;
  610. var i = 0x8000;
  611. var info = LunarDateArray[year - MinYear] & 0x0FFFF;
  612. //计算12个月中有多少天为30天
  613. for (int m = 0; m < 12; m++)
  614. {
  615. var f = info & i;
  616. if (f != 0)
  617. {
  618. sumDay++;
  619. }
  620. i >>= 1;
  621. }
  622. return sumDay + GetChineseLeapMonthDays(year);
  623. }
  624. /// <summary>
  625. /// 获得当前时间的时辰
  626. /// </summary>
  627. /// <param name="dt"></param>
  628. /// <returns></returns>
  629. ///
  630. private string GetChineseHour(DateTime dt)
  631. {
  632. //计算时辰的地支
  633. var hour = dt.Hour;
  634. var minute = dt.Minute;
  635. if (minute != 0)
  636. {
  637. hour += 1;
  638. }
  639. var offset = hour / 2;
  640. if (offset >= 12)
  641. {
  642. offset = 0;
  643. }
  644. //计算天干
  645. var ts = Date - GanZhiStartDay;
  646. var i = ts.Days % 60;
  647. var indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1;
  648. var tmpGan = TianGan.Substring(indexGan) + TianGan.Substring(0, indexGan + 2);
  649. return tmpGan[offset].ToString() + DiZhi[offset];
  650. }
  651. /// <summary>
  652. /// 检查农历日期是否合理
  653. /// </summary>
  654. /// <param name="year"></param>
  655. /// <param name="month"></param>
  656. /// <param name="day"></param>
  657. /// <param name="leapMonth"></param>
  658. private void CheckChineseDateLimit(int year, int month, int day, bool leapMonth)
  659. {
  660. if (year < MinYear || year > MaxYear)
  661. {
  662. throw new ChineseCalendarException("非法农历日期");
  663. }
  664. if (month < 1 || month > 12)
  665. {
  666. throw new ChineseCalendarException("非法农历日期");
  667. }
  668. if (day < 1 || day > 30) //中国的月最多30天
  669. {
  670. throw new ChineseCalendarException("非法农历日期");
  671. }
  672. int leap = GetChineseLeapMonth(year); // 计算该年应该闰哪个月
  673. if (leapMonth && month != leap)
  674. {
  675. throw new ChineseCalendarException("非法农历日期");
  676. }
  677. }
  678. /// <summary>
  679. /// 将0-9转成汉字形式
  680. /// </summary>
  681. /// <param name="n"></param>
  682. /// <returns></returns>
  683. private string ConvertNumToChineseNum(char n)
  684. {
  685. if (n < '0' || n > '9')
  686. {
  687. return default;
  688. }
  689. return n switch
  690. {
  691. '0' => HzNum[0].ToString(),
  692. '1' => HzNum[1].ToString(),
  693. '2' => HzNum[2].ToString(),
  694. '3' => HzNum[3].ToString(),
  695. '4' => HzNum[4].ToString(),
  696. '5' => HzNum[5].ToString(),
  697. '6' => HzNum[6].ToString(),
  698. '7' => HzNum[7].ToString(),
  699. '8' => HzNum[8].ToString(),
  700. '9' => HzNum[9].ToString(),
  701. _ => default
  702. };
  703. }
  704. /// <summary>
  705. /// 测试某位是否为真
  706. /// </summary>
  707. /// <param name="num"></param>
  708. /// <param name="bitpostion"></param>
  709. /// <returns></returns>
  710. private bool BitTest32(int num, int bitpostion)
  711. {
  712. if (bitpostion > 31 || bitpostion < 0)
  713. {
  714. throw new ArgumentException("参数错误: 取值范围0-31", nameof(bitpostion));
  715. }
  716. int bit = 1 << bitpostion;
  717. return (num & bit) != 0;
  718. }
  719. /// <summary>
  720. /// 将星期几转成数字表示
  721. /// </summary>
  722. /// <param name="dayOfWeek"></param>
  723. /// <returns></returns>
  724. private int ConvertDayOfWeek(DayOfWeek dayOfWeek)
  725. {
  726. return dayOfWeek switch
  727. {
  728. DayOfWeek.Sunday => 1,
  729. DayOfWeek.Monday => 2,
  730. DayOfWeek.Tuesday => 3,
  731. DayOfWeek.Wednesday => 4,
  732. DayOfWeek.Thursday => 5,
  733. DayOfWeek.Friday => 6,
  734. DayOfWeek.Saturday => 7,
  735. _ => 0
  736. };
  737. }
  738. /// <summary>
  739. /// 比较当天是不是指定的第周几
  740. /// </summary>
  741. /// <param name="date"></param>
  742. /// <param name="month"></param>
  743. /// <param name="week"></param>
  744. /// <param name="day"></param>
  745. /// <returns></returns>
  746. private bool CompareWeekDayHoliday(DateTime date, int month, int week, int day)
  747. {
  748. bool result = false;
  749. if (date.Month == month) //月份相同
  750. {
  751. if (ConvertDayOfWeek(date.DayOfWeek) == day) //星期几相同
  752. {
  753. DateTime firstDay = new DateTime(date.Year, date.Month, 1); //生成当月第一天
  754. int i = ConvertDayOfWeek(firstDay.DayOfWeek);
  755. int firWeekDays = 7 - ConvertDayOfWeek(firstDay.DayOfWeek) + 1; //计算第一周剩余天数
  756. if (i > day)
  757. {
  758. if ((week - 1) * 7 + day + firWeekDays == date.Day)
  759. {
  760. result = true;
  761. }
  762. }
  763. else
  764. {
  765. if (day + firWeekDays + (week - 2) * 7 == date.Day)
  766. {
  767. result = true;
  768. }
  769. }
  770. }
  771. }
  772. return result;
  773. }
  774. #endregion
  775. #region 节日
  776. /// <summary>
  777. /// 计算中国农历节日
  778. /// </summary>
  779. public string ChineseCalendarHoliday
  780. {
  781. get
  782. {
  783. string tempStr = "";
  784. if (IsChineseLeapMonth)
  785. {
  786. return tempStr;
  787. }
  788. foreach (var date in LunarHolidayInfo)
  789. {
  790. var end = date.Recess > 0 ? date.Day + date.Recess - 1 : date.Day + date.Recess;
  791. if (date.Month != ChineseMonth || ChineseDay < date.Day || ChineseDay > end)
  792. {
  793. continue;
  794. }
  795. tempStr = date.HolidayName;
  796. break;
  797. }
  798. //对除夕进行特别处理
  799. if (ChineseMonth != 12)
  800. {
  801. return tempStr;
  802. }
  803. int i = GetChineseMonthDays(ChineseYear, 12); //计算当年农历12月的总天数
  804. if (ChineseDay == i) //如果为最后一天
  805. {
  806. tempStr = "除夕";
  807. }
  808. return tempStr;
  809. }
  810. }
  811. /// <summary>
  812. /// 按某月第几周第几日计算的节日
  813. /// </summary>
  814. public string WeekDayHoliday
  815. {
  816. get
  817. {
  818. string tempStr = "";
  819. foreach (var wh in WHolidayInfo)
  820. {
  821. if (!CompareWeekDayHoliday(Date, wh.Month, wh.WeekAtMonth, wh.WeekDay))
  822. {
  823. continue;
  824. }
  825. tempStr = wh.HolidayName;
  826. break;
  827. }
  828. return tempStr;
  829. }
  830. }
  831. /// <summary>
  832. /// 按公历日计算的节日
  833. /// </summary>
  834. public string DateHoliday
  835. {
  836. get
  837. {
  838. string tempStr = "";
  839. foreach (DateInfoStruct sh in SolarHolidayInfo)
  840. {
  841. var end = sh.Recess > 0 ? sh.Day + sh.Recess - 1 : sh.Day + sh.Recess;
  842. if ((sh.Month == Date.Month) && Date.Day >= sh.Day && Date.Day <= end)
  843. {
  844. tempStr = sh.HolidayName;
  845. break;
  846. }
  847. if (CustomHolidays.Keys.Any(d => d.Date == Date))
  848. {
  849. tempStr = CustomHolidays[Date];
  850. break;
  851. }
  852. }
  853. return tempStr;
  854. }
  855. }
  856. /// <summary>
  857. /// 今天是否是假期
  858. /// </summary>
  859. public bool IsHoliday => !IsWorkDay;
  860. /// <summary>
  861. /// 今天是否是工作日
  862. /// </summary>
  863. public bool IsWorkDay
  864. {
  865. get
  866. {
  867. bool isHoliday = false;
  868. foreach (var date in SolarHolidayInfo)
  869. {
  870. var end = date.Recess > 0 ? date.Day + date.Recess - 1 : date.Day + date.Recess;
  871. if ((date.Month == Date.Month) && Date.Day >= date.Day && Date.Day <= end && date.Recess > 0)
  872. {
  873. isHoliday = true;
  874. break;
  875. }
  876. if (CustomHolidays.Keys.Any(d => d.Date == Date))
  877. {
  878. isHoliday = true;
  879. break;
  880. }
  881. }
  882. if (!isHoliday)
  883. {
  884. foreach (DateInfoStruct lh in LunarHolidayInfo)
  885. {
  886. var end = lh.Recess > 0 ? lh.Day + lh.Recess - 1 : lh.Day + lh.Recess;
  887. if (lh.Month == ChineseMonth && ChineseDay >= lh.Day && ChineseDay <= end && lh.Recess > 0)
  888. {
  889. isHoliday = true;
  890. break;
  891. }
  892. }
  893. //对除夕进行特别处理
  894. if (ChineseMonth == 12)
  895. {
  896. int i = GetChineseMonthDays(ChineseYear, 12); //计算当年农历12月的总天数
  897. if (ChineseDay == i) //如果为最后一天
  898. {
  899. isHoliday = true;
  900. }
  901. }
  902. }
  903. return !isHoliday && !IsWeekend() || CustomWorkDays.Any(s => s.Date == Date);
  904. }
  905. }
  906. /// <summary>
  907. /// 是否是周末
  908. /// </summary>
  909. /// <returns></returns>
  910. private bool IsWeekend()
  911. {
  912. return Date.DayOfWeek == DayOfWeek.Saturday || Date.DayOfWeek == DayOfWeek.Sunday;
  913. }
  914. #endregion
  915. #region 公历日期
  916. /// <summary>
  917. /// 取对应的公历日期
  918. /// </summary>
  919. public DateTime Date { get; set; }
  920. /// <summary>
  921. /// 取星期几
  922. /// </summary>
  923. public DayOfWeek WeekDay => Date.DayOfWeek;
  924. /// <summary>
  925. /// 周几的字符
  926. /// </summary>
  927. public string WeekDayStr => Date.DayOfWeek switch
  928. {
  929. DayOfWeek.Sunday => "星期日",
  930. DayOfWeek.Monday => "星期一",
  931. DayOfWeek.Tuesday => "星期二",
  932. DayOfWeek.Wednesday => "星期三",
  933. DayOfWeek.Thursday => "星期四",
  934. DayOfWeek.Friday => "星期五",
  935. _ => "星期六"
  936. };
  937. /// <summary>
  938. /// 公历日期中文表示法 如一九九七年七月一日
  939. /// </summary>
  940. public string DateString => "公元" + Date.ToLongDateString();
  941. /// <summary>
  942. /// 当前是否公历闰年
  943. /// </summary>
  944. public bool IsLeapYear => DateTime.IsLeapYear(Date.Year);
  945. /// <summary>
  946. /// 28星宿计算
  947. /// </summary>
  948. public string ChineseConstellation
  949. {
  950. get
  951. {
  952. var ts = Date - ChineseConstellationReferDay;
  953. var offset = ts.Days;
  954. var modStarDay = offset % 28;
  955. return (modStarDay >= 0 ? ChineseConstellationName[modStarDay] : ChineseConstellationName[27 + modStarDay]);
  956. }
  957. }
  958. /// <summary>
  959. /// 时辰
  960. /// </summary>
  961. public string ChineseHour => GetChineseHour(_datetime);
  962. #endregion
  963. #region 农历日期
  964. /// <summary>
  965. /// 农历今天
  966. /// </summary>
  967. public static ChineseCalendar Today => new ChineseCalendar(DateTime.Today);
  968. /// <summary>
  969. /// 是否闰月
  970. /// </summary>
  971. public bool IsChineseLeapMonth { get; private set; }
  972. /// <summary>
  973. /// 当年是否有闰月
  974. /// </summary>
  975. public bool IsChineseLeapYear { get; private set; }
  976. /// <summary>
  977. /// 农历日
  978. /// </summary>
  979. public int ChineseDay { get; private set; }
  980. /// <summary>
  981. /// 农历日中文表示
  982. /// </summary>
  983. public string ChineseDayString => ChineseDay switch
  984. {
  985. 0 => "",
  986. 10 => "初十",
  987. 20 => "二十",
  988. 30 => "三十",
  989. _ => (NStr2[ChineseDay / 10] + NStr1[ChineseDay % 10].ToString())
  990. };
  991. /// <summary>
  992. /// 农历的月份
  993. /// </summary>
  994. public int ChineseMonth { get; private set; }
  995. /// <summary>
  996. /// 农历月份字符串
  997. /// </summary>
  998. public string ChineseMonthString => MonthString[ChineseMonth];
  999. /// <summary>
  1000. /// 取农历年份
  1001. /// </summary>
  1002. public int ChineseYear { get; private set; }
  1003. /// <summary>
  1004. /// 取农历年字符串如,一九九七年
  1005. /// </summary>
  1006. public string ChineseYearString
  1007. {
  1008. get
  1009. {
  1010. string tempStr = "";
  1011. string num = ChineseYear.ToString();
  1012. for (int i = 0; i < 4; i++)
  1013. {
  1014. tempStr += ConvertNumToChineseNum(num[i]);
  1015. }
  1016. return tempStr + "年";
  1017. }
  1018. }
  1019. /// <summary>
  1020. /// 取农历日期表示法:农历一九九七年正月初五
  1021. /// </summary>
  1022. public string ChineseDateString
  1023. {
  1024. get
  1025. {
  1026. if (IsChineseLeapMonth)
  1027. {
  1028. return ChineseYearString + "闰" + ChineseMonthString + ChineseDayString;
  1029. }
  1030. return ChineseYearString + ChineseMonthString + ChineseDayString;
  1031. }
  1032. }
  1033. /// <summary>
  1034. /// 定气法计算二十四节气,二十四节气是按地球公转来计算的,并非是阴历计算的
  1035. /// </summary>
  1036. /// <remarks>
  1037. /// 节气的定法有两种。古代历法采用的称为"恒气",即按时间把一年等分为24份,
  1038. /// 每一节气平均得15天有余,所以又称"平气"。现代农历采用的称为"定气",即
  1039. /// 按地球在轨道上的位置为标准,一周360°,两节气之间相隔15°。由于冬至时地
  1040. /// 球位于近日点附近,运动速度较快,因而太阳在黄道上移动15°的时间不到15天。
  1041. /// 夏至前后的情况正好相反,太阳在黄道上移动较慢,一个节气达16天之多。采用
  1042. /// 定气时可以保证春、秋两分必然在昼夜平分的那两天。
  1043. /// </remarks>
  1044. public string ChineseTwentyFourDay
  1045. {
  1046. get
  1047. {
  1048. var baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1049. string tempStr = "";
  1050. var y = Date.Year;
  1051. for (int i = 1; i <= 24; i++)
  1052. {
  1053. var num = 525948.76 * (y - 1900) + STermInfo[i - 1];
  1054. var newDate = baseDateAndTime.AddMinutes(num);
  1055. if (newDate.DayOfYear != Date.DayOfYear)
  1056. {
  1057. continue;
  1058. }
  1059. tempStr = SolarTerm[i - 1];
  1060. break;
  1061. }
  1062. return tempStr;
  1063. }
  1064. }
  1065. /// <summary>
  1066. /// 当前日期前一个最近节气
  1067. /// </summary>
  1068. public string ChineseTwentyFourPrevDay
  1069. {
  1070. get
  1071. {
  1072. DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1073. string tempStr = "";
  1074. var y = Date.Year;
  1075. for (int i = 24; i >= 1; i--)
  1076. {
  1077. var num = 525948.76 * (y - 1900) + STermInfo[i - 1];
  1078. var newDate = baseDateAndTime.AddMinutes(num);
  1079. if (newDate.DayOfYear < Date.DayOfYear)
  1080. {
  1081. tempStr = $"{SolarTerm[i - 1]}[{newDate:yyyy-MM-dd}]";
  1082. break;
  1083. }
  1084. }
  1085. return tempStr;
  1086. }
  1087. }
  1088. /// <summary>
  1089. /// 当前日期后一个最近节气
  1090. /// </summary>
  1091. public string ChineseTwentyFourNextDay
  1092. {
  1093. get
  1094. {
  1095. var baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0); //#1/6/1900 2:05:00 AM#
  1096. string tempStr = "";
  1097. var y = Date.Year;
  1098. for (int i = 1; i <= 24; i++)
  1099. {
  1100. var num = 525948.76 * (y - 1900) + STermInfo[i - 1];
  1101. var newDate = baseDateAndTime.AddMinutes(num);
  1102. if (newDate.DayOfYear > Date.DayOfYear)
  1103. {
  1104. tempStr = $"{SolarTerm[i - 1]}[{newDate:yyyy-MM-dd}]";
  1105. break;
  1106. }
  1107. }
  1108. return tempStr;
  1109. }
  1110. }
  1111. #endregion
  1112. #region 星座
  1113. /// <summary>
  1114. /// 计算指定日期的星座序号
  1115. /// </summary>
  1116. /// <returns></returns>
  1117. public string Constellation
  1118. {
  1119. get
  1120. {
  1121. int index;
  1122. var m = Date.Month;
  1123. var d = Date.Day;
  1124. var y = m * 100 + d;
  1125. if (y >= 321 && y <= 419)
  1126. {
  1127. index = 0;
  1128. }
  1129. else if (y >= 420 && y <= 520)
  1130. {
  1131. index = 1;
  1132. }
  1133. else if (y >= 521 && y <= 620)
  1134. {
  1135. index = 2;
  1136. }
  1137. else if (y >= 621 && y <= 722)
  1138. {
  1139. index = 3;
  1140. }
  1141. else if (y >= 723 && y <= 822)
  1142. {
  1143. index = 4;
  1144. }
  1145. else if (y >= 823 && y <= 922)
  1146. {
  1147. index = 5;
  1148. }
  1149. else if (y >= 923 && y <= 1022)
  1150. {
  1151. index = 6;
  1152. }
  1153. else if (y >= 1023 && y <= 1121)
  1154. {
  1155. index = 7;
  1156. }
  1157. else if (y >= 1122 && y <= 1221)
  1158. {
  1159. index = 8;
  1160. }
  1161. else if (y >= 1222 || y <= 119)
  1162. {
  1163. index = 9;
  1164. }
  1165. else if (y >= 120 && y <= 218)
  1166. {
  1167. index = 10;
  1168. }
  1169. else if (y >= 219 && y <= 320)
  1170. {
  1171. index = 11;
  1172. }
  1173. else
  1174. {
  1175. index = 0;
  1176. }
  1177. return ConstellationName[index];
  1178. }
  1179. }
  1180. #endregion
  1181. #region 生肖
  1182. /// <summary>
  1183. /// 计算属相的索引,注意虽然属相是以农历年来区别的,但是目前在实际使用中是按公历来计算的
  1184. /// 鼠年为1,其它类推
  1185. /// </summary>
  1186. public int Animal
  1187. {
  1188. get
  1189. {
  1190. int offset = Date.Year - AnimalStartYear;
  1191. return offset % 12 + 1;
  1192. }
  1193. }
  1194. /// <summary>
  1195. /// 取属相字符串
  1196. /// </summary>
  1197. public string AnimalString
  1198. {
  1199. get
  1200. {
  1201. int offset = Date.Year - AnimalStartYear; //阳历计算
  1202. return AnimalStr[offset % 12].ToString();
  1203. }
  1204. }
  1205. #endregion
  1206. #region 天干地支
  1207. /// <summary>
  1208. /// 取农历年的干支表示法如 乙丑年
  1209. /// </summary>
  1210. public string GanZhiYearString
  1211. {
  1212. get
  1213. {
  1214. int i = (ChineseYear - GanZhiStartYear) % 60; //计算干支
  1215. var tempStr = TianGan[i % 10] + DiZhi[i % 12].ToString() + "年";
  1216. return tempStr;
  1217. }
  1218. }
  1219. /// <summary>
  1220. /// 取干支的月表示字符串,注意农历的闰月不记干支
  1221. /// </summary>
  1222. public string GanZhiMonthString
  1223. {
  1224. get
  1225. {
  1226. //每个月的地支总是固定的,而且总是从寅月开始
  1227. int zhiIndex;
  1228. if (ChineseMonth > 10)
  1229. {
  1230. zhiIndex = ChineseMonth - 10;
  1231. }
  1232. else
  1233. {
  1234. zhiIndex = ChineseMonth + 2;
  1235. }
  1236. var zhi = DiZhi[zhiIndex - 1].ToString();
  1237. //根据当年的干支年的干来计算月干的第一个
  1238. int ganIndex = 1;
  1239. int i = (ChineseYear - GanZhiStartYear) % 60; //计算干支
  1240. ganIndex = (i % 10) switch
  1241. {
  1242. 0 => 3, //甲
  1243. 1 => 5, //乙
  1244. 2 => 7, //丙
  1245. 3 => 9, //丁
  1246. 4 => 1, //戊
  1247. 5 => 3, //己
  1248. 6 => 5, //庚
  1249. 7 => 7, //辛
  1250. 8 => 9, //壬
  1251. 9 => 1, //癸
  1252. _ => ganIndex
  1253. };
  1254. var gan = TianGan[(ganIndex + ChineseMonth - 2) % 10].ToString();
  1255. return gan + zhi + "月";
  1256. }
  1257. }
  1258. /// <summary>
  1259. /// 取干支日表示法
  1260. /// </summary>
  1261. public string GanZhiDayString
  1262. {
  1263. get
  1264. {
  1265. var ts = Date - GanZhiStartDay;
  1266. var offset = ts.Days;
  1267. var i = offset % 60;
  1268. return TianGan[i % 10].ToString() + DiZhi[i % 12] + "日";
  1269. }
  1270. }
  1271. /// <summary>
  1272. /// 取当前日期的干支表示法如 甲子年乙丑月丙庚日
  1273. /// </summary>
  1274. public string GanZhiDateString => GanZhiYearString + GanZhiMonthString + GanZhiDayString;
  1275. #endregion
  1276. /// <summary>
  1277. /// 取下一天
  1278. /// </summary>
  1279. /// <returns></returns>
  1280. public ChineseCalendar NextDay => new ChineseCalendar(Date.AddDays(1));
  1281. /// <summary>
  1282. /// 取前一天
  1283. /// </summary>
  1284. /// <returns></returns>
  1285. public ChineseCalendar PervDay => new ChineseCalendar(Date.AddDays(-1));
  1286. /// <summary>
  1287. /// 取下n天
  1288. /// </summary>
  1289. /// <returns></returns>
  1290. public ChineseCalendar AddDays(int days)
  1291. {
  1292. return new ChineseCalendar(Date.AddDays(days));
  1293. }
  1294. /// <summary>
  1295. /// 取下n天
  1296. /// </summary>
  1297. /// <returns></returns>
  1298. public ChineseCalendar AddWorkDays(int days)
  1299. {
  1300. var cc = new ChineseCalendar(Date);
  1301. while (true)
  1302. {
  1303. cc = cc.AddDays(1);
  1304. if (cc.IsWorkDay)
  1305. {
  1306. days--;
  1307. }
  1308. if (days == 0)
  1309. {
  1310. return cc;
  1311. }
  1312. }
  1313. }
  1314. /// <summary>
  1315. /// 加n月
  1316. /// </summary>
  1317. /// <returns></returns>
  1318. public ChineseCalendar AddMonths(int months)
  1319. {
  1320. return new ChineseCalendar(Date.AddMonths(months));
  1321. }
  1322. }
  1323. }