LunarCalendarHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Common
  8. {
  9. public class LunarCalendarHelper
  10. {
  11. //C# 获取农历日期
  12. ///<summary>
  13. /// 实例化一个 ChineseLunisolarCalendar
  14. ///</summary>
  15. private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
  16. ///<summary>
  17. /// 十天干
  18. ///</summary>
  19. private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
  20. ///<summary>
  21. /// 十二地支
  22. ///</summary>
  23. private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
  24. ///<summary>
  25. /// 十二生肖
  26. ///</summary>
  27. private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  28. ///<summary>
  29. /// 返回农历天干地支年
  30. ///</summary>
  31. ///<param name="year">农历年</param>
  32. ///<return s></return s>
  33. public static string GetLunisolarYear(int year)
  34. {
  35. if (year > 3)
  36. {
  37. int tgIndex = (year - 4) % 10;
  38. int dzIndex = (year - 4) % 12;
  39. return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
  40. }
  41. throw new ArgumentOutOfRangeException("无效的年份!");
  42. }
  43. ///<summary>
  44. /// 农历月
  45. ///</summary>
  46. ///<return s></return s>
  47. private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
  48. ///<summary>
  49. /// 农历日
  50. ///</summary>
  51. private static string[] days1 = { "初", "十", "廿", "三" };
  52. ///<summary>
  53. /// 农历日
  54. ///</summary>
  55. private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
  56. private static int[] daysInt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  57. ///<summary>
  58. /// 返回农历月
  59. ///</summary>
  60. ///<param name="month">月份</param>
  61. ///<return s></return s>
  62. public static string GetLunisolarMonth(int month)
  63. {
  64. if (month < 13 && month > 0)
  65. {
  66. return months[month - 1];
  67. }
  68. throw new ArgumentOutOfRangeException("无效的月份!");
  69. }
  70. ///<summary>
  71. /// 返回农历日
  72. ///</summary>
  73. ///<param name="day">天</param>
  74. ///<return s></return s>
  75. public static string GetLunisolarDay(int day)
  76. {
  77. if (day > 0 && day < 32)
  78. {
  79. if (day != 20 && day != 30)
  80. {
  81. return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
  82. }
  83. else
  84. {
  85. return string.Concat(days[(day - 1) / 10], days1[1]);
  86. }
  87. }
  88. throw new ArgumentOutOfRangeException("无效的日!");
  89. }
  90. ///<summary>
  91. /// 根据公历获取农历日期
  92. ///</summary>
  93. ///<param name="datetime">公历日期</param>
  94. ///<return s></return s>
  95. public static string GetChineseDateTime(DateTime datetime)
  96. {
  97. int year = ChineseCalendar.GetYear(datetime);
  98. int month = ChineseCalendar.GetMonth(datetime);
  99. int day = ChineseCalendar.GetDayOfMonth(datetime);
  100. //获取闰月, 0 则表示没有闰月
  101. int leapMonth = ChineseCalendar.GetLeapMonth(year);
  102. bool isleap = false;
  103. if (leapMonth > 0)
  104. {
  105. if (leapMonth == month)
  106. {
  107. //闰月
  108. isleap = true;
  109. month--;
  110. }
  111. else if (month > leapMonth)
  112. {
  113. month--;
  114. }
  115. }
  116. return string.Concat(isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
  117. }
  118. public static bool IsHaveNewYear(DateTime datetime)
  119. {
  120. int year = ChineseCalendar.GetYear(datetime);
  121. int month = ChineseCalendar.GetMonth(datetime);
  122. int day = ChineseCalendar.GetDayOfMonth(datetime);
  123. //获取闰月, 0 则表示没有闰月
  124. int leapMonth = ChineseCalendar.GetLeapMonth(year);
  125. bool isleap = false;
  126. if (leapMonth > 0)
  127. {
  128. if (leapMonth == month)
  129. {
  130. //闰月
  131. isleap = true;
  132. month--;
  133. }
  134. else if (month > leapMonth)
  135. {
  136. month--;
  137. }
  138. }
  139. if (GetLunisolarMonth(month) != "正")
  140. {
  141. return true;
  142. }
  143. else
  144. {
  145. if (new List<string> { "初一", "初二", "初三", "初四", "初五", "初六", "初七" }.Contains(GetLunisolarDay(day)))
  146. {
  147. return false;
  148. }
  149. else
  150. {
  151. return true;
  152. }
  153. }
  154. }
  155. }
  156. }