index.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import dayjs from "dayjs";
  2. interface LunarDateDetail {
  3. solarDate: string; // 阳历日期
  4. lunarYear: number; // 农历年份
  5. lunarMon: number; // 农历月份
  6. lunarDay: number; // 农历日期
  7. isLeap: boolean; // 是否闰月
  8. yearCyl: string; // 年柱,天干地支表示的年份
  9. monCyl: string; // 月柱,天干地支表示的月份
  10. dayCyl: string; // 日柱,天干地支表示的日期
  11. zodiac: string; // 生肖
  12. lunarYearCN: string; // 农历年份的中文写法
  13. lunarMonCN: string; // 农历月份的中文写法
  14. lunarDayCN: string; // 农历日期的中文写法
  15. }
  16. /**
  17. * LUNAR_INFO 数组值的计算原理:
  18. *
  19. * 每个值使用 16 进制表示,包括以下部分:
  20. * 1. 前 4 位:表示闰月的月份,如果没有闰月为 0。
  21. * 2. 中间 12 位:表示 1 到 12 月的大小月,1 为大月(30 天),0 为小月(29 天)。
  22. * 3. 后 4 位:表示闰月的天数,如果没有闰月为 0。
  23. *
  24. * 以 `0x04bd8` 为例:
  25. * - `0x` 表示这是一个 16 进制数。
  26. * - `04bd8` 是具体的 16 进制值。
  27. *
  28. * 转换为二进制后,`04bd8` 为 `0000 0100 1011 1101 1000`:
  29. * 1. 前 4 位 `0000`:表示该年份没有闰月(若有闰月,该值为闰月的月份)。
  30. * 2. 中间 12 位 `0100 1011 1101`:从左到右分别表示 1 到 12 月的天数。`1` 表示大月(30 天),`0` 表示小月(29 天)。
  31. * - `0`(1月):小月(29 天)
  32. * - `1`(2月):大月(30 天)
  33. * - `0`(3月):小月(29 天)
  34. * - `1`(4月):大月(30 天)
  35. * - `0`(5月):小月(29 天)
  36. * - `1`(6月):大月(30 天)
  37. * - `1`(7月):大月(30 天)
  38. * - `1`(8月):大月(30 天)
  39. * - `1`(9月):大月(30 天)
  40. * - `1`(10月):大月(30 天)
  41. * - `0`(11月):小月(29 天)
  42. * - `0`(12月):小月(29 天)
  43. * 3. 后 4 位 `1000`:表示闰月的天数。如果前 4 位为 `0000`(即没有闰月),则这一部分不使用。
  44. */
  45. const LUNAR_INFO: number[] = [
  46. 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2, //1900-1909
  47. 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977, //1910-1919
  48. 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, //1920-1929
  49. 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950, //1930-1939
  50. 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, //1940-1949
  51. 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0, //1950-1959
  52. 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0, //1960-1969
  53. 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6, //1970-1979
  54. 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570, //1980-1989
  55. 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, //1990-1999
  56. 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5, //2000-2009
  57. 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, //2010-2019
  58. 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530, //2020-2029
  59. 0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, //2030-2039
  60. 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0, //2040-2049
  61. 0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0, //2050-2059
  62. 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4, //2060-2069
  63. 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0, //2070-2079
  64. 0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160, //2080-2089
  65. 0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252, //2090-2099
  66. 0x0d520 //2100
  67. ];
  68. const CHINESE_NUMBER = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  69. const NUMBER_MONTH: string[] = ["正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"];
  70. const NUMBER_1: string[] = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
  71. const NUMBER_2: string[] = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"];
  72. const ZODIACS: string[] = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"];
  73. /**
  74. * 获取指定农历年的天数
  75. * @param y 年份
  76. * @returns 农历年天数
  77. */
  78. const lunarYearDays = (y: number): number => {
  79. let sum = 348;
  80. for (let i = 0x8000; i > 0x8; i >>= 1) {
  81. sum += (LUNAR_INFO[y - 1900] & i) !== 0 ? 1 : 0;
  82. }
  83. return sum + yearLeapDays(y);
  84. }
  85. /**
  86. * 获取指定年份的闰月月份
  87. * @param y 年份
  88. * @returns 闰月月份,非闰年返回0
  89. */
  90. const yearLeapMonth = (y: number): number => LUNAR_INFO[y - 1900] & 0xf;
  91. /**
  92. * 获取指定年份的闰月天数
  93. * @param y 年份
  94. * @returns 闰月天数,非闰年返回0
  95. */
  96. const yearLeapDays = (y: number): number => yearLeapMonth(y) ? ((LUNAR_INFO[y - 1900] & 0x10000) !== 0 ? 30 : 29) : 0;
  97. /**
  98. * 获取天干地支表示的月份或日期
  99. * @param num 月份或日期的数值
  100. * @returns 天干地支表示
  101. */
  102. const cyclicalm = (num: number): string => NUMBER_1[num % 10] + NUMBER_2[num % 12];
  103. /**
  104. * 获取指定年月的阴历天数
  105. * @param y 农历年份
  106. * @param m 农历月份
  107. * @returns 月份天数
  108. */
  109. const monthDays = (y: number, m: number): number => (LUNAR_INFO[y - 1900] & (0x10000 >> m)) === 0 ? 29 : 30;
  110. /**
  111. * 获取指定年份的生肖
  112. * @param y 农历年份
  113. * @returns 生肖
  114. */
  115. const getYearZodiac = (y: number): string => ZODIACS[(y - 4) % 12];
  116. /**
  117. * 获取指定日期的农历表示
  118. * @param day 日期
  119. * @returns 农历表示
  120. */
  121. const getDateCN = (day: number): string => {
  122. const prefixes = ["初", "十", "廿", "三十"];
  123. if (day === 10) return "初十";
  124. if (day === 20) return "二十";
  125. if (day === 30) return "三十";
  126. const tensPlace = Math.floor(day / 10);
  127. const unitsPlace = day % 10;
  128. return prefixes[tensPlace] + (unitsPlace ? CHINESE_NUMBER[unitsPlace] : "");
  129. }
  130. /**
  131. * 获取指定农历年份的天干地支表示
  132. * @param lunarYear 农历年份
  133. * @returns 天干地支表示
  134. */
  135. const getLunarYearText = (lunarYear: number): string => {
  136. return `${NUMBER_1[(lunarYear - 4) % 10]}${NUMBER_2[(lunarYear - 4) % 12]}年`;
  137. }
  138. /**
  139. * 获取指定范围内的所有农历年份 信息
  140. * @param startYear 起始农历年份
  141. * @param endYear 结束农历年份
  142. * @returns 农历年份列表
  143. */
  144. const getYears = (startYear: number, endYear: number) => {
  145. const years = [];
  146. for (let i = startYear; i <= endYear; i++) {
  147. years.push({
  148. year: i,
  149. lunarYear: getLunarYearText(i),
  150. lunarYearCN: i.toString().split('').map(i => CHINESE_NUMBER[Number(i)]).join('')
  151. });
  152. }
  153. return years;
  154. }
  155. /**
  156. * 获取指定年份的所有农历月份
  157. * @param year 年份
  158. * @returns 农历闰月月份
  159. */
  160. export const getYearLeapMonth = (year: number) => {
  161. const leap = yearLeapMonth(year)
  162. return {
  163. year,
  164. leapMonth: leap || undefined,
  165. leapMonthCN: leap ? `闰${NUMBER_MONTH[leap - 1]}月` : undefined,
  166. days: leap ? (LUNAR_INFO[year - 1900] & 0x10000) !== 0 ? 30 : 29 : 0
  167. };
  168. }
  169. /**
  170. * 计算指定日期的农历元素
  171. * @param date 指定日期
  172. * @returns 农历信息
  173. */
  174. export const getLunarDate = (date: dayjs.ConfigType): LunarDateDetail => {
  175. const lunarDate: number[] = new Array(7).fill(0);
  176. let temp = 0;
  177. let leap = 0;
  178. const baseDate = dayjs(new Date(1900, 0, 31));
  179. const objDate = dayjs(date);
  180. let offset = objDate.diff(baseDate, "day");
  181. lunarDate[5] = offset + 40; // 日柱,从1900-01-31开始的天数计算
  182. lunarDate[4] = 14; // 月柱,从1900-01-31开始的月数计算
  183. let i = 1900;
  184. for (; i < 2100 && offset > 0; i++) {
  185. temp = lunarYearDays(i);
  186. offset -= temp;
  187. lunarDate[4] += 12; // 每经过一年增加12个月柱
  188. }
  189. if (offset < 0) {
  190. offset += temp;
  191. i--;
  192. lunarDate[4] -= 12;
  193. }
  194. lunarDate[0] = i; // 农历年份
  195. lunarDate[3] = i - 1864; // 年柱,甲子从1864年开始
  196. leap = yearLeapMonth(i); // 闰哪个月
  197. lunarDate[6] = 0; // 闰月标记,初始为0
  198. for (let j = 1; j < 13 && offset > 0; j++) {
  199. if (leap > 0 && j === (leap + 1) && lunarDate[6] === 0) {
  200. --j;
  201. lunarDate[6] = 1;
  202. temp = yearLeapDays(i);
  203. } else {
  204. temp = monthDays(i, j);
  205. }
  206. if (lunarDate[6] === 1 && j === (leap + 1)) {
  207. lunarDate[6] = 0;
  208. }
  209. offset -= temp;
  210. if (lunarDate[6] === 0) {
  211. lunarDate[4]++;
  212. }
  213. lunarDate[1] = j; // 农历月份
  214. }
  215. if (offset === 0 && leap > 0 && lunarDate[6] === 1) {
  216. lunarDate[6] = 0;
  217. } else if (offset < 0) {
  218. offset += temp;
  219. lunarDate[1]--;
  220. lunarDate[4]--;
  221. }
  222. lunarDate[2] = offset + 1; // 农历日期
  223. return {
  224. solarDate: objDate.format('YYYY-MM-DD'), // 公历日期
  225. lunarYear: lunarDate[0], // 农历年份
  226. lunarMon: lunarDate[1] + 1, // 农历月份
  227. lunarDay: lunarDate[2], // 农历日期
  228. isLeap: Boolean(lunarDate[6]), // 是否闰月
  229. zodiac: getYearZodiac(lunarDate[0]), // 生肖
  230. yearCyl: cyclicalm(lunarDate[3]), // 年柱
  231. monCyl: cyclicalm(lunarDate[4]), // 月柱
  232. dayCyl: cyclicalm(lunarDate[5]), // 日柱
  233. lunarYearCN: `${lunarDate[0].toString().split('').map(i => CHINESE_NUMBER[Number(i)]).join('')}`, // 农历年份中文表示
  234. lunarMonCN: `${NUMBER_MONTH[lunarDate[1]]}月`, // 农历月份中文表示
  235. lunarDayCN: getDateCN(lunarDate[2]) // 农历日期中文表示
  236. };
  237. }
  238. console.log(Date.now())
  239. // 2057-9-28
  240. console.log(getLunarDate('2057-09-28'))
  241. // 农历丁丑(牛)年八月三十
  242. // 2097-8-7
  243. console.log(getLunarDate('2097-08-07'))
  244. // 农历丁巳(蛇)年七月一
  245. // 非闰月
  246. console.log(getLunarDate('2001-04-27'))
  247. // 闰月
  248. console.log(getLunarDate('2001-05-27'))
  249. console.log(getYearLeapMonth(2001))
  250. console.log(getYears(2000, 2003))
  251. console.log(Date.now())