TKLunarCalendarHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. namespace YiSha.Util
  6. {
  7. public class TKLunarCalendarHelper
  8. {
  9. //C# 获取农历日期
  10. ///<summary>
  11. /// 实例化一个 ChineseLunisolarCalendar
  12. ///</summary>
  13. private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
  14. ///<summary>
  15. /// 十天干
  16. ///</summary>
  17. private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
  18. ///<summary>
  19. /// 十二地支
  20. ///</summary>
  21. private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
  22. ///<summary>
  23. /// 十二生肖
  24. ///</summary>
  25. private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  26. ///<summary>
  27. /// 返回农历天干地支年
  28. ///</summary>
  29. ///<param name="year">农历年</param>
  30. ///<return s></return s>
  31. public static string GetLunisolarYear(int year)
  32. {
  33. if (year > 3)
  34. {
  35. int tgIndex = (year - 4) % 10;
  36. int dzIndex = (year - 4) % 12;
  37. return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
  38. }
  39. throw new ArgumentOutOfRangeException("无效的年份!");
  40. }
  41. ///<summary>
  42. /// 农历月
  43. ///</summary>
  44. ///<return s></return s>
  45. private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
  46. ///<summary>
  47. /// 农历日
  48. ///</summary>
  49. private static string[] days1 = { "初", "十", "廿", "三" };
  50. ///<summary>
  51. /// 农历日
  52. ///</summary>
  53. private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
  54. private static int[] daysInt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  55. ///<summary>
  56. /// 返回农历月
  57. ///</summary>
  58. ///<param name="month">月份</param>
  59. ///<return s></return s>
  60. public static string GetLunisolarMonth(int month)
  61. {
  62. if (month < 13 && month > 0)
  63. {
  64. return months[month - 1];
  65. }
  66. throw new ArgumentOutOfRangeException("无效的月份!");
  67. }
  68. ///<summary>
  69. /// 返回农历日
  70. ///</summary>
  71. ///<param name="day">天</param>
  72. ///<return s></return s>
  73. public static string GetLunisolarDay(int day)
  74. {
  75. if (day > 0 && day < 32)
  76. {
  77. if (day != 20 && day != 30)
  78. {
  79. return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
  80. }
  81. else
  82. {
  83. return string.Concat(days[(day - 1) / 10], days1[1]);
  84. }
  85. }
  86. throw new ArgumentOutOfRangeException("无效的日!");
  87. }
  88. ///<summary>
  89. /// 根据公历获取农历日期
  90. ///</summary>
  91. ///<param name="datetime">公历日期</param>
  92. ///<return s></return s>
  93. public static string GetChineseDateTime(DateTime datetime)
  94. {
  95. int year = ChineseCalendar.GetYear(datetime);
  96. int month = ChineseCalendar.GetMonth(datetime);
  97. int day = ChineseCalendar.GetDayOfMonth(datetime);
  98. //获取闰月, 0 则表示没有闰月
  99. int leapMonth = ChineseCalendar.GetLeapMonth(year);
  100. bool isleap = false;
  101. if (leapMonth > 0)
  102. {
  103. if (leapMonth == month)
  104. {
  105. //闰月
  106. isleap = true;
  107. month--;
  108. }
  109. else if (month > leapMonth)
  110. {
  111. month--;
  112. }
  113. }
  114. return string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
  115. }
  116. public static bool IsHaveNewYear(DateTime datetime)
  117. {
  118. int year = ChineseCalendar.GetYear(datetime);
  119. int month = ChineseCalendar.GetMonth(datetime);
  120. int day = ChineseCalendar.GetDayOfMonth(datetime);
  121. //获取闰月, 0 则表示没有闰月
  122. int leapMonth = ChineseCalendar.GetLeapMonth(year);
  123. bool isleap = false;
  124. if (leapMonth > 0)
  125. {
  126. if (leapMonth == month)
  127. {
  128. //闰月
  129. isleap = true;
  130. month--;
  131. }
  132. else if (month > leapMonth)
  133. {
  134. month--;
  135. }
  136. }
  137. if (GetLunisolarMonth(month) != "正")
  138. {
  139. return true;
  140. }
  141. else
  142. {
  143. if (new List<string> { "初一", "初二", "初三", "初四", "初五", "初六", "初七" }.Contains(GetLunisolarDay(day)))
  144. {
  145. return false;
  146. }
  147. else
  148. {
  149. return true;
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// 获取除夕日期
  155. /// </summary>
  156. /// <param name="year"></param>
  157. /// <returns></returns>
  158. public static DateTime GetNewYear(int year)
  159. {
  160. DateTime result = new DateTime();
  161. try
  162. {
  163. if (year < 1900 || year > 2100)
  164. {
  165. Exception e = new Exception("参数年份时间不在支持的范围内,支持范围1900~2100年");
  166. throw e;
  167. }
  168. //摘要:在1月21日-2月20日的31天中,历史上都有做过春节的经历,其中1月21日最少为1次,1月23日、2月6日、2月10日、2月17日最多为7次。
  169. DateTime t1 = new DateTime(year, 1, 21);
  170. DateTime t2 = new DateTime(year + 1, 2, 20);
  171. result = t1;
  172. ChineseLunisolarCalendar cc = new ChineseLunisolarCalendar();
  173. int t = t1.CompareTo(t2);
  174. while (t1.CompareTo(t2) != 0)
  175. {
  176. if (cc.GetMonth(t1) == 1 && cc.GetDayOfMonth(t1) == 1)
  177. {
  178. result = t1;
  179. }
  180. t1 = t1.AddDays(1);
  181. }
  182. return result;
  183. }
  184. catch (Exception err)
  185. {
  186. }
  187. return result;
  188. }
  189. }
  190. }