lunar.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * 参考了eleworld.com上的算法,并修正了5处节气错误
  3. * 中国农历算法 - 实用于公历 1901 年至 2100 年之间的 200 年
  4. */
  5. const ChineseCalendars = [
  6. 0x00, 0x04, 0xad, 0x08, 0x5a, 0x01, 0xd5, 0x54, 0xb4, 0x09, 0x64, 0x05, 0x59, 0x45,
  7. 0x95, 0x0a, 0xa6, 0x04, 0x55, 0x24, 0xad, 0x08, 0x5a, 0x62, 0xda, 0x04, 0xb4, 0x05,
  8. 0xb4, 0x55, 0x52, 0x0d, 0x94, 0x0a, 0x4a, 0x2a, 0x56, 0x02, 0x6d, 0x71, 0x6d, 0x01,
  9. 0xda, 0x02, 0xd2, 0x52, 0xa9, 0x05, 0x49, 0x0d, 0x2a, 0x45, 0x2b, 0x09, 0x56, 0x01,
  10. 0xb5, 0x20, 0x6d, 0x01, 0x59, 0x69, 0xd4, 0x0a, 0xa8, 0x05, 0xa9, 0x56, 0xa5, 0x04,
  11. 0x2b, 0x09, 0x9e, 0x38, 0xb6, 0x08, 0xec, 0x74, 0x6c, 0x05, 0xd4, 0x0a, 0xe4, 0x6a,
  12. 0x52, 0x05, 0x95, 0x0a, 0x5a, 0x42, 0x5b, 0x04, 0xb6, 0x04, 0xb4, 0x22, 0x6a, 0x05,
  13. 0x52, 0x75, 0xc9, 0x0a, 0x52, 0x05, 0x35, 0x55, 0x4d, 0x0a, 0x5a, 0x02, 0x5d, 0x31,
  14. 0xb5, 0x02, 0x6a, 0x8a, 0x68, 0x05, 0xa9, 0x0a, 0x8a, 0x6a, 0x2a, 0x05, 0x2d, 0x09,
  15. 0xaa, 0x48, 0x5a, 0x01, 0xb5, 0x09, 0xb0, 0x39, 0x64, 0x05, 0x25, 0x75, 0x95, 0x0a,
  16. 0x96, 0x04, 0x4d, 0x54, 0xad, 0x04, 0xda, 0x04, 0xd4, 0x44, 0xb4, 0x05, 0x54, 0x85,
  17. 0x52, 0x0d, 0x92, 0x0a, 0x56, 0x6a, 0x56, 0x02, 0x6d, 0x02, 0x6a, 0x41, 0xda, 0x02,
  18. 0xb2, 0xa1, 0xa9, 0x05, 0x49, 0x0d, 0x0a, 0x6d, 0x2a, 0x09, 0x56, 0x01, 0xad, 0x50,
  19. 0x6d, 0x01, 0xd9, 0x02, 0xd1, 0x3a, 0xa8, 0x05, 0x29, 0x85, 0xa5, 0x0c, 0x2a, 0x09,
  20. 0x96, 0x54, 0xb6, 0x08, 0x6c, 0x09, 0x64, 0x45, 0xd4, 0x0a, 0xa4, 0x05, 0x51, 0x25,
  21. 0x95, 0x0a, 0x2a, 0x72, 0x5b, 0x04, 0xb6, 0x04, 0xac, 0x52, 0x6a, 0x05, 0xd2, 0x0a,
  22. 0xa2, 0x4a, 0x4a, 0x05, 0x55, 0x94, 0x2d, 0x0a, 0x5a, 0x02, 0x75, 0x61, 0xb5, 0x02,
  23. 0x6a, 0x03, 0x61, 0x45, 0xa9, 0x0a, 0x4a, 0x05, 0x25, 0x25, 0x2d, 0x09, 0x9a, 0x68,
  24. 0xda, 0x08, 0xb4, 0x09, 0xa8, 0x59, 0x54, 0x03, 0xa5, 0x0a, 0x91, 0x3a, 0x96, 0x04,
  25. 0xad, 0xb0, 0xad, 0x04, 0xda, 0x04, 0xf4, 0x62, 0xb4, 0x05, 0x54, 0x0b, 0x44, 0x5d,
  26. 0x52, 0x0a, 0x95, 0x04, 0x55, 0x22, 0x6d, 0x02, 0x5a, 0x71, 0xda, 0x02, 0xaa, 0x05,
  27. 0xb2, 0x55, 0x49, 0x0b, 0x4a, 0x0a, 0x2d, 0x39, 0x36, 0x01, 0x6d, 0x80, 0x6d, 0x01,
  28. 0xd9, 0x02, 0xe9, 0x6a, 0xa8, 0x05, 0x29, 0x0b, 0x9a, 0x4c, 0xaa, 0x08, 0xb6, 0x08,
  29. 0xb4, 0x38, 0x6c, 0x09, 0x54, 0x75, 0xd4, 0x0a, 0xa4, 0x05, 0x45, 0x55, 0x95, 0x0a,
  30. 0x9a, 0x04, 0x55, 0x44, 0xb5, 0x04, 0x6a, 0x82, 0x6a, 0x05, 0xd2, 0x0a, 0x92, 0x6a,
  31. 0x4a, 0x05, 0x55, 0x0a, 0x2a, 0x4a, 0x5a, 0x02, 0xb5, 0x02, 0xb2, 0x31, 0x69, 0x03,
  32. 0x31, 0x73, 0xa9, 0x0a, 0x4a, 0x05, 0x2d, 0x55, 0x2d, 0x09, 0x5a, 0x01, 0xd5, 0x48,
  33. 0xb4, 0x09, 0x68, 0x89, 0x54, 0x0b, 0xa4, 0x0a, 0xa5, 0x6a, 0x95, 0x04, 0xad, 0x08,
  34. 0x6a, 0x44, 0xda, 0x04, 0x74, 0x05, 0xb0, 0x25, 0x54, 0x03
  35. ]
  36. const BigLeapMonthYears = [
  37. //大闰月的闰年年份
  38. 6, 14, 19, 25, 33, 36, 38, 41, 44, 52, 55, 79, 117, 136, 147, 150, 155, 158, 185, 193
  39. ]
  40. const DaysInGregorianMonths = [
  41. //公历每个月的天数
  42. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  43. ]
  44. const SectionalTermMap = [
  45. [7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5],
  46. [5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 3, 3, 4, 4, 3, 3, 3],
  47. [6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5],
  48. [5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 4, 4, 5, 5, 4, 4, 4, 5, 4, 4, 4, 4, 5],
  49. [6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5],
  50. [6, 6, 7, 7, 6, 6, 6, 7, 6, 6, 6, 6, 5, 6, 6, 6, 5, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 5, 4, 5, 5, 5, 5],
  51. [7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 6, 6, 6, 7, 7],
  52. [8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7],
  53. [8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 7],
  54. [9, 9, 9, 9, 8, 9, 9, 9, 8, 8, 9, 9, 8, 8, 8, 9, 8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 8],
  55. [8, 8, 8, 8, 7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 7],
  56. [7, 8, 8, 8, 7, 7, 8, 8, 7, 7, 7, 8, 7, 7, 7, 7, 6, 7, 7, 7, 6, 6, 7, 7, 6, 6, 6, 7, 7]
  57. ]
  58. const SectionalTermYear = [
  59. [13, 49, 85, 117, 149, 185, 201, 250, 250],
  60. [13, 45, 81, 117, 149, 185, 201, 250, 250],
  61. [13, 48, 84, 112, 148, 184, 200, 201, 250],
  62. [13, 45, 76, 108, 140, 172, 200, 201, 250],
  63. [13, 44, 72, 104, 132, 168, 200, 201, 250],
  64. [5, 33, 68, 96, 124, 152, 188, 200, 201],
  65. [29, 57, 85, 120, 148, 176, 200, 201, 250],
  66. [13, 48, 76, 104, 132, 168, 196, 200, 201],
  67. [25, 60, 88, 120, 148, 184, 200, 201, 250],
  68. [16, 44, 76, 108, 144, 172, 200, 201, 250],
  69. [28, 60, 92, 124, 160, 192, 200, 201, 250],
  70. [17, 53, 85, 124, 156, 188, 200, 201, 250]
  71. ]
  72. const PrincipleTermMap = [
  73. [21, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 20, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20],
  74. [20, 19, 19, 20, 20, 19, 19, 19, 19, 19, 19, 19, 19, 18, 19, 19, 19, 18, 18, 19, 19, 18, 18, 18, 18, 18, 18, 18],
  75. [21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 20],
  76. [20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 20, 20, 20, 20, 19, 20, 20, 20, 19, 19, 20, 20, 19, 19, 19, 20, 20],
  77. [21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 20, 20, 20, 21, 21],
  78. [22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 20, 21, 21, 21, 20, 20, 21, 21, 21],
  79. [23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 23],
  80. [23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23],
  81. [23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 23],
  82. [24, 24, 24, 24, 23, 24, 24, 24, 23, 23, 24, 24, 23, 23, 23, 24, 23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 23],
  83. [23, 23, 23, 23, 22, 23, 23, 23, 22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 22],
  84. [22, 22, 23, 23, 22, 22, 22, 23, 22, 22, 22, 22, 21, 22, 22, 22, 21, 21, 22, 22, 21, 21, 21, 22, 21, 21, 21, 21, 22]
  85. ]
  86. const PrincipleTermYear = [
  87. [13, 45, 81, 113, 149, 185, 201],
  88. [21, 57, 93, 125, 161, 193, 201],
  89. [21, 56, 88, 120, 152, 188, 200, 201],
  90. [21, 49, 81, 116, 144, 176, 200, 201],
  91. [17, 49, 77, 112, 140, 168, 200, 201],
  92. [28, 60, 88, 116, 148, 180, 200, 201],
  93. [25, 53, 84, 112, 144, 172, 200, 201],
  94. [29, 57, 89, 120, 148, 180, 200, 201],
  95. [17, 45, 73, 108, 140, 168, 200, 201],
  96. [28, 60, 92, 124, 160, 192, 200, 201],
  97. [16, 44, 80, 112, 148, 180, 200, 201],
  98. [17, 53, 88, 120, 156, 188, 200, 201]
  99. ]
  100. const StemNames = '甲乙丙丁戊己庚辛壬癸'
  101. const BranchNames = '子丑寅卯辰巳午未申酉戌亥'
  102. const ZodiacSigns = '鼠牛虎兔龙蛇马羊猴鸡狗猪'
  103. const LunarMonths = '正二三四五六七八九十冬腊'
  104. const PrincipleTermNames = ["大寒", "雨水", "春分", "谷雨", "小满", "夏至", "大暑", "处暑", "秋分", "霜降", "小雪", "冬至"]
  105. const SectionalTermNames = ["小寒", "立春", "惊蛰", "清明", "立夏", "芒种", "小暑", "立秋", "白露", "寒露", "立冬", "大雪"]
  106. const ChineseNums = '十一二三四五六七八九十'
  107. //星座
  108. // const Astro = "\u9b54\u7faf\u6c34\u74f6\u53cc\u9c7c\u767d\u7f8a\u91d1\u725b\u53cc\u5b50\u5de8\u87f9\u72ee\u5b50\u5904\u5973\u5929\u79e4\u5929\u874e\u5c04\u624b\u9b54\u7faf"
  109. // const AstroDays = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22]
  110. const BaseDate = {
  111. // 初始日,公历农历对应日期: 公历 1901 年 1 月 1 日,对应农历 4598 年 11 月 11 日
  112. year: 1901,
  113. month: 1,
  114. day: 1,
  115. index: 0,
  116. chineseYear: 4598 - 1,
  117. chineseMonth: 11,
  118. chineseDate: 11
  119. }
  120. const SolarTermRevises = [
  121. { solar: SectionalTermNames[2], year: 2014, month: 3, wrong: 5, correct: 6 },
  122. { solar: PrincipleTermNames[2], year: 2051, month: 3, wrong: 21, correct: 20 },
  123. { solar: SectionalTermNames[1], year: 2083, month: 2, wrong: 4, correct: 3 },
  124. { solar: PrincipleTermNames[2], year: 2084, month: 3, wrong: 20, correct: 19 },
  125. { solar: SectionalTermNames[5], year: 2094, month: 6, wrong: 6, correct: 5 }
  126. ]
  127. const isGregorianLeapYear = gregorianYear => {
  128. let isLeap = false
  129. if (gregorianYear % 4 == 0) isLeap = true
  130. if (gregorianYear % 100 == 0) isLeap = false
  131. if (gregorianYear % 400 == 0) isLeap = true
  132. return isLeap
  133. }
  134. const daysInGregorianMonth = (y, m) => {
  135. let d = DaysInGregorianMonths[m - 1]
  136. if (m == 2 && isGregorianLeapYear(y)) d++ // 公历闰年二月多一天
  137. return d
  138. }
  139. const dayOfYear = (y, m, d) => {
  140. let c = 0
  141. for (let i = 1; i < m; i++) {
  142. c = c + daysInGregorianMonth(y, i)
  143. }
  144. c = c + d
  145. return c
  146. }
  147. const dayOfWeek = (y, m, d) => {
  148. let w = 1 // 公历一年一月一日是星期一,所以起始值为星期日
  149. y = (y - 1) % 400 + 1 // 公历星期值分部 400 年循环一次
  150. let ly = (y - 1) / 4 // 闰年次数
  151. ly = ly - (y - 1) / 100
  152. ly = ly + (y - 1) / 400
  153. let ry = y - 1 - ly // 常年次数
  154. w = w + ry // 常年星期值增一
  155. w = w + 2 * ly // 闰年星期值增二
  156. w = w + dayOfYear(y, m, d)
  157. w = (w - 1) % 7 + 1
  158. return w
  159. }
  160. const daysInChineseMonth = (y, m) => {
  161. // 注意:闰月 m < 0
  162. let index = y - BaseDate.chineseYear + BaseDate.index
  163. let v = 0
  164. let l = 0
  165. let d = 30
  166. if (1 <= m && m <= 8) {
  167. v = ChineseCalendars[2 * index]
  168. l = m - 1
  169. if (((v >> l) & 0x01) == 1) d = 29
  170. } else if (9 <= m && m <= 12) {
  171. v = ChineseCalendars[2 * index + 1]
  172. l = m - 9
  173. if (((v >> l) & 0x01) == 1) d = 29
  174. } else {
  175. v = ChineseCalendars[2 * index + 1]
  176. v = (v >> 4) & 0x0F
  177. if (v != Math.abs(m)) {
  178. d = 0
  179. } else {
  180. d = 29
  181. for (let i = 0; i < BigLeapMonthYears.length; i++) {
  182. if (BigLeapMonthYears[i] == index) {
  183. d = 30
  184. break
  185. }
  186. }
  187. }
  188. }
  189. return d
  190. }
  191. const nextChineseMonth = (y, m) => {
  192. let n = Math.abs(m) + 1
  193. if (m > 0) {
  194. let index = y - BaseDate.chineseYear + BaseDate.index;
  195. let v = ChineseCalendars[2 * index + 1]
  196. v = (v >> 4) & 0x0F
  197. if (v == m) n = -m
  198. }
  199. if (n == 13) n = 1
  200. return n
  201. }
  202. const sectionalTerm = (y, m) => {
  203. if (y < 1901 || y > 2100) return 0
  204. let index = 0
  205. let ry = y - BaseDate.year + 1
  206. while (ry >= SectionalTermYear[m - 1][index]) { index++ }
  207. return SectionalTermMap[m - 1][4 * index + ry % 4]
  208. }
  209. const principleTerm = (y, m) => {
  210. if (y < 1901 || y > 2100) return 0
  211. let index = 0
  212. let ry = y - BaseDate.year + 1
  213. while (ry >= PrincipleTermYear[m - 1][index]) { index++ }
  214. return PrincipleTermMap[m - 1][4 * index + ry % 4]
  215. }
  216. const checkSolarTerm = (gregorianYear, gregorianMonth, gregorianDay) => {
  217. let filterYear = SolarTermRevises.filter(item => item.year == gregorianYear)
  218. if (filterYear.length > 0) {
  219. filterYear = filterYear[0]
  220. if (filterYear.month == gregorianMonth) {
  221. if (filterYear.wrong == gregorianDay) return { correct: false, type: 'wroung' }
  222. if (filterYear.correct == gregorianDay) return { correct: false, type: 'revise', solar: filterYear.solar }
  223. }
  224. }
  225. return { correct: true }
  226. }
  227. const lunarYear = chineseYear => StemNames[(chineseYear - 1) % 10] + BranchNames[(chineseYear - 1) % 12] + ZodiacSigns[(chineseYear - 1) % 12] + '年'
  228. const lunarMonth = chineseMonth => chineseMonth > 0 ? `${ LunarMonths[chineseMonth - 1] }月` : `闰${ LunarMonths[-chineseMonth - 1] }月`
  229. const lunarDay = chineseDay => {
  230. if (chineseDay < 1 || chineseDay > 30) return ''
  231. if (chineseDay <= 10) return `初${ChineseNums[chineseDay]}`
  232. else if (chineseDay < 20) return `十${ ChineseNums[chineseDay % 10] }`
  233. else if (chineseDay == 20) return `二十`
  234. else if (chineseDay < 30) return `廿${ ChineseNums[chineseDay % 10] }`
  235. return '三十'
  236. }
  237. // //获取星座
  238. // const getAstro = (gregorianMonth, gregorianDay) => Astro.substr(gregorianMonth * 2 - (gregorianDay < AstroDays[gregorianMonth - 1] ? 2 : 0), 2) + "\u5ea7"
  239. class LunarDate {
  240. constructor(gregorianYear, gregorianMonth, gregorianDay) {
  241. this.initGregorianYear = gregorianYear
  242. this.initGregorianMonth = gregorianMonth
  243. this.initGregorianDay = gregorianDay
  244. this.setGregorian(gregorianYear, gregorianMonth, gregorianDay)
  245. }
  246. resetInitGregorian(gregorianYear, gregorianMonth, gregorianDay) {
  247. this.initGregorianYear = gregorianYear
  248. this.initGregorianMonth = gregorianMonth
  249. this.initGregorianDay = gregorianDay
  250. }
  251. setGregorian(gregorianYear, gregorianMonth = 1, gregorianDay = 1) {
  252. this.gregorianYear = gregorianYear
  253. this.gregorianMonth = gregorianMonth
  254. this.gregorianDate = gregorianDay
  255. this.isGregorianLeap = isGregorianLeapYear(gregorianYear)
  256. this.dayOfYear = dayOfYear(gregorianYear, gregorianMonth, gregorianDay)
  257. this.dayOfWeek = dayOfWeek(gregorianYear, gregorianMonth, gregorianDay)
  258. this.reviseInfo = checkSolarTerm(gregorianYear, gregorianMonth, gregorianDay)
  259. this.computeChineseFields()
  260. this.computeSolarTerms()
  261. }
  262. getReviseInfo() {
  263. return this.reviseInfo
  264. }
  265. computeChineseFields() {
  266. if (this.gregorianYear < 1901 || this.gregorianYear > 2100) return 1
  267. const { year, month, day, chineseYear, chineseMonth, chineseDate } = BaseDate
  268. let startYear = year
  269. let startMonth = month
  270. let startDate = day
  271. this.chineseYear = chineseYear
  272. this.chineseMonth = chineseMonth
  273. this.chineseDate = chineseDate
  274. if (this.gregorianYear >= 2000) {
  275. // 第二个对应日,用以提高计算效率
  276. // 公历 2000 年 1 月 1 日,对应农历 4697 年 11 月 25 日
  277. startYear = year + 99
  278. startMonth = 1
  279. startDate = 1
  280. this.chineseYear = chineseYear + 99
  281. this.chineseMonth = 11
  282. this.chineseDate = 25
  283. }
  284. let daysDiff = 0
  285. for (let i = startYear; i < this.gregorianYear; i++) {
  286. daysDiff += 365
  287. if (isGregorianLeapYear(i)) daysDiff += 1
  288. }
  289. for (let i = startMonth; i < this.gregorianMonth; i++) {
  290. daysDiff += daysInGregorianMonth(this.gregorianYear, i)
  291. }
  292. daysDiff += this.gregorianDate - startDate
  293. this.chineseDate += daysDiff
  294. let lastDate = daysInChineseMonth(this.chineseYear, this.chineseMonth)
  295. let nextMonth = nextChineseMonth(this.chineseYear, this.chineseMonth)
  296. while (this.chineseDate > lastDate) {
  297. if (Math.abs(nextMonth) < Math.abs(this.chineseMonth)) { this.chineseYear++ }
  298. this.chineseMonth = nextMonth
  299. this.chineseDate -= lastDate
  300. lastDate = daysInChineseMonth(this.chineseYear, this.chineseMonth)
  301. nextMonth = nextChineseMonth(this.chineseYear, this.chineseMonth)
  302. }
  303. return 0
  304. }
  305. computeSolarTerms() {
  306. if (this.gregorianYear < 1901 || this.gregorianYear > 2100) return 1
  307. this.sectionalTerm = sectionalTerm(this.gregorianYear, this.gregorianMonth)
  308. this.principleTerm = principleTerm(this.gregorianYear, this.gregorianMonth)
  309. return 0
  310. }
  311. getLunarDate() {
  312. this.setGregorian(this.initGregorianYear, 1, 1)
  313. const _lunarYear = this.chineseYear + 1
  314. this.setGregorian(this.initGregorianYear, this.initGregorianMonth, this.initGregorianDay)
  315. let lunar_day = ''
  316. let lunar_type = ''
  317. let lunar_date = lunarDay(this.chineseDate)
  318. if (this.reviseInfo.correct) {
  319. if (this.gregorianDate == this.sectionalTerm) {
  320. lunar_day = SectionalTermNames[this.gregorianMonth - 1]
  321. lunar_type = 'solar'
  322. } else if (this.gregorianDate == this.principleTerm) {
  323. lunar_day = PrincipleTermNames[this.gregorianMonth - 1]
  324. lunar_type = 'solar'
  325. } else if (this.chineseDate == 1 && this.chineseMonth > 0) {
  326. lunar_day = LunarMonths[this.chineseMonth - 1] + '月'
  327. } else if (this.chineseDate == 1 && this.chineseMonth < 0) {
  328. lunar_day = '闰' + LunarMonths[-this.chineseMonth - 1] + '月'
  329. } else {
  330. lunar_day = lunar_date
  331. }
  332. } else {
  333. if (this.reviseInfo.type == 'wroung') {
  334. if (this.chineseDate == 1 && this.chineseMonth > 0) {
  335. lunar_day = LunarMonths[this.chineseMonth - 1] + '月'
  336. } else if (this.chineseDate == 1 && this.chineseMonth < 0) {
  337. lunar_day = '闰' + LunarMonths[-this.chineseMonth - 1] + '月'
  338. } else {
  339. lunar_day = lunar_date
  340. }
  341. } else {
  342. lunar_day = this.reviseInfo.solar
  343. lunar_type = 'solar'
  344. }
  345. }
  346. return {
  347. year: this.gregorianYear,
  348. month: this.gregorianMonth,
  349. day: this.gregorianDate,
  350. lunar_order: _lunarYear,
  351. lunar_year: lunarYear(_lunarYear),
  352. lunar_month: lunarMonth(this.chineseMonth),
  353. lunar_day,
  354. lunar_date,
  355. lunar_type,
  356. lm: this.chineseMonth,
  357. ld: this.chineseDate,
  358. // astro: getAstro(this.gregorianMonth, this.gregorianDate)
  359. }
  360. }
  361. }
  362. module.exports.Lunar = new LunarDate(2000, 1, 1)