写在前面,本文章是对JS日期转农历的一点记录,如有错误或者更好见解欢迎指出和讨论
发布 2021-03-06
最新修正 2021-03-12(考虑闰月存在闰正月和闰腊月更新部分算法和注释)
最新修正 2021-08-20(修改初始化年月日参数名写错)
这里先简单介绍农历表中十六进制规则方便后面转换的了解
农历数据和节气数据没有固定的日期一般是由天文台观测数据得来,例如:紫金山天文台.香港天文台
举个例子1949年农历对应十六进制0x0b557
高位 0000 1011 0101 0101 0111 低位
0000
1011
0101
0101
0111
20-17
16-13
12-9
8-5
4-1
高位[20-17]代表闰月大小,0001代表大闰月30天,0000代表小闰月29天(当且低位存在闰月有效)
中间位[16-5]代表一月到十二月大小月,其中1代表大月30天,0代表小月29天 (注:其中一月对应16位,十二月对应5位)
低位[4-1]代表闰月所在的月份,上述例子7代表的就是闰月在七月,若无闰月就为0
整理农历对应天数表格
1月
2月
3月
4月
5月
6月
7月
润7月
8月
9月
10月
11月
12月
30
29
30
30
29
30
29
29
30
29
30
29
30
// 农历查询表
const lunarYearArr = new Array(
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,//1900-1909
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,//1910-1919
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,//1920-1929
0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,//1930-1939
0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,//1940-1949
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,//1950-1959
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,//1960-1969
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,//1970-1979
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,//1980-1989
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,//1990-1999
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,//2000-2009
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,//2010-2019
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,//2020-2029
0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,//2030-2039
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,//2040-2049
0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0,//2050-2059
0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4,//2060-2069
0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0,//2070-2079
0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160,//2080-2089
0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252,//2090-2099
0x0d520 //2100
);
计算农历和部分基础方法
// 格式化日期
const week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
const formatDate = function(time, type){
const date = new Date(time);
const y = date.getFullYear();
const m = date.getMonth()+1>=1){
let monthDays = (ly & i) ? 30 : 29;
totalDays += monthDays;
}
//考虑是否有闰月天数
if(hasLeapMonth(ly)){
totalDays += leapMonthDays(ly);
}
return totalDays
}
/**
* 计算农历每个月的天数, 参数为存储农历年的16进制
* @param {Number} ly
* @return {Array}
* @eg {Function} lunarYearMonths(0x0b557)
*/
const lunarYearMonths = function(ly){
//从高位第16位(1月)起向右移至低位第5位(12月)添加数组每项
let monthArr = [];
for(let i=0x8000; i>0x8; i>>=1){
monthArr.push((ly & i) ? 30 : 29);
}
//考虑是否有闰月天数
if(hasLeapMonth(ly)){
monthArr.splice(hasLeapMonth(ly), 0, leapMonthDays(ly));
}
return monthArr
}
// 将农历年转换为天干, 参数为存储农历年的16进制
const getTianGan = function(ly){
let tianGanKey = (ly - 3) % 10;
if(tianGanKey === 0) tianGanKey = 10;
return tianGan[tianGanKey - 1]
}
// 将农历年转换为地支, 参数为存储农历年的16进制
const getDiZhi = function(ly){
let diZhiKey = (ly - 3) % 12;
if(diZhiKey === 0) diZhiKey = 12;
return diZhi[diZhiKey - 1]
}
实现方法入口
/**
* 主要实现方法入口, 参数为字符串格式日期或者时间戳(毫秒)
* @param {String | Number} date
* @return {Object}
* @eg {Function} sloarToLunar("1949-01-29") || sloarToLunar(660268800000)
*/
// 天干
const tianGan = new Array("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸");
// 地支
const diZhi = new Array("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥");
// 农历月
const lunarMonth = new Array("正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊");
// 农历日
const lunarDay = new Array("一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "初", "廿");
const sloarToLunar = function(date){
const time = formatDate(date, true);
// 初始化农历年月日
let ly, lm, ld;
// 初始化传入年月日
let sy = time.year;
let sm = time.month;
let sd = time.day;
if(sy = 2100) return {code: 400, msg: "输入年限不在查询表范围", status: false};
// 计算与基准相差天数
let differenceDay = (Date.UTC(Number(sy), Number(sm), Number(sd)) - BENCHMARK_TIME) / 86400000; //24*60*60*1000;
//计算农历年份
for(let y=0; y{
let str = item.split(" ");
if(str[0] == `${lm 1){
sd -= 1;
}
else{
sm -= 1;
sd = 31;
if(sm < 0){
sy -= 1;
sm = 11;
}
}
return solarTermStr;
}
获取星座方法(以下未定义参数名对应入口方法里的参数)
// 初始化星座数组
const constellation = new Array(
{s: "0120", e: "0218", c: "水瓶座"},
{s: "0219", e: "0320", c: "双鱼座"},
{s: "0321", e: "0419", c: "白羊座"},
{s: "0420", e: "0520", c: "金牛座"},
{s: "0521", e: "0621", c: "双子座"},
{s: "0622", e: "0722", c: "巨蟹座"},
{s: "0723", e: "0822", c: "狮子座"},
{s: "0823", e: "0922", c: "处女座"},
{s: "0923", e: "1023", c: "天秤座"},
{s: "1024", e: "1122", c: "天蝎座"},
{s: "1123", e: "1221", c: "射手座"},
{s: "1222", e: "0119", c: "摩羯座"}
);
// 计算星座
// sy sm sd 为传入年月日
let constellationStr = null;
let c = sm * 100 + sd; //把传入日期数据转化为与数组中可对比的数字
constellation.forEach((item, index)=>{
if(!constellationStr){
if(c >= Number(item.s) && c = Number(item.s) || c |