ChineseCalendar.cs 47 KB

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