ChineseCalendar.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Globalization;
  3. namespace Masuit.Tools.DateTimeExt
  4. {
  5. /// <summary>
  6. /// 中国农历年处理类用net自带类
  7. /// </summary>
  8. public static class ChineseCalendar
  9. {
  10. /// <summary>
  11. /// 实例化一个 ChineseLunisolarCalendar
  12. /// </summary>
  13. private static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar();
  14. /// <summary>
  15. /// 获取农历当前日期
  16. /// </summary>
  17. /// <returns>当前农历日期</returns>
  18. public static string GetChineseDateTimeNow(this DateTime dt) => GetChineseDateTime(dt);
  19. /// <summary>
  20. /// 根据公历获取农历日期
  21. /// </summary>
  22. /// <param name="datetime">公历日期</param>
  23. /// <returns>公历日期的字符串形式</returns>
  24. public static string GetChineseDateTime(this DateTime datetime)
  25. {
  26. int lyear = cCalendar.GetYear(datetime);
  27. int lmonth = cCalendar.GetMonth(datetime);
  28. int lday = cCalendar.GetDayOfMonth(datetime);
  29. //获取闰月, 0 则表示没有闰月
  30. int leapMonth = cCalendar.GetLeapMonth(lyear);
  31. bool isleap = false;
  32. if (leapMonth > 0)
  33. {
  34. if (leapMonth == lmonth)
  35. {
  36. //闰月
  37. isleap = true;
  38. lmonth--;
  39. }
  40. else if (lmonth > leapMonth)
  41. {
  42. lmonth--;
  43. }
  44. }
  45. return string.Concat(GetLunisolarYear(lyear), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday));
  46. }
  47. /// <summary>
  48. /// 返回农历日期
  49. /// </summary>
  50. public static string Now => DateTime.Now.GetChineseDateTimeNow();
  51. /// <summary>
  52. /// 最大支持日期
  53. /// </summary>
  54. public static DateTime MaxSupportedDateTime => cCalendar.MaxSupportedDateTime;
  55. /// <summary>
  56. /// 最小支持日期
  57. /// </summary>
  58. public static DateTime MinSupportedDateTime { get; } = cCalendar.MinSupportedDateTime;
  59. /// <summary>
  60. /// 返回生肖
  61. /// </summary>
  62. /// <param name="datetime">公历日期</param>
  63. /// <returns>生肖</returns>
  64. public static string GetShengXiao(this DateTime datetime) => shengxiao[cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(datetime)) - 1];
  65. #region 农历年
  66. /// <summary>
  67. /// 十天干
  68. /// </summary>
  69. private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
  70. /// <summary>
  71. /// 十二地支
  72. /// </summary>
  73. private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
  74. /// <summary>
  75. /// 十二生肖
  76. /// </summary>
  77. private static string[] shengxiao = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
  78. /// <summary>
  79. /// 返回农历天干地支年
  80. /// </summary>
  81. /// <param name="year">农历年</param>
  82. /// <exception cref="ArgumentOutOfRangeException"></exception>
  83. /// <returns>天干地支年</returns>
  84. public static string GetLunisolarYear(int year)
  85. {
  86. if (year > 3)
  87. {
  88. int tgIndex = (year - 4) % 10;
  89. int dzIndex = (year - 4) % 12;
  90. return string.Concat(tiangan[tgIndex], dizhi[dzIndex], "[", shengxiao[dzIndex], "]");
  91. }
  92. throw new ArgumentOutOfRangeException("无效的年份!");
  93. }
  94. #endregion
  95. #region 农历月
  96. /// <summary>
  97. /// 农历月
  98. /// </summary>
  99. private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
  100. /// <summary>
  101. /// 返回农历月
  102. /// </summary>
  103. /// <param name="month">月份</param>
  104. /// <exception cref="ArgumentOutOfRangeException"></exception>
  105. /// <returns>农历月</returns>
  106. public static string GetLunisolarMonth(int month)
  107. {
  108. if (month < 13 && month > 0)
  109. {
  110. return months[month - 1];
  111. }
  112. throw new ArgumentOutOfRangeException("无效的月份!");
  113. }
  114. #endregion
  115. #region 农历日
  116. private static string[] days1 = { "初", "十", "廿", "三" };
  117. /// <summary>
  118. /// 日
  119. /// </summary>
  120. private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
  121. /// <summary>
  122. /// 返回农历日
  123. /// </summary>
  124. /// <param name="day">阳历日</param>
  125. /// <exception cref="ArgumentOutOfRangeException"></exception>
  126. /// <returns>农历日</returns>
  127. public static string GetLunisolarDay(int day)
  128. {
  129. if (day > 0 && day < 32)
  130. {
  131. if (day != 20 && day != 30)
  132. {
  133. return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
  134. }
  135. return string.Concat(days[(day - 1) / 10], days1[1]);
  136. }
  137. throw new ArgumentOutOfRangeException("无效的日!");
  138. }
  139. #endregion
  140. }
  141. }