ipink-util
Version:
util.js
1,167 lines (1,164 loc) • 30 kB
JavaScript
import { getWeek, formatDate } from './date.mjs';
let lunarMonth = ["正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "冬", "腊"], lunarDay = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "初", "廿"], diZhi = ["子鼠", "丑牛", "寅虎", "卯兔", "辰龙", "巳蛇", "午马", "未羊", "申猴", "酉鸡", "戌狗", "亥猪"];
const sloarToLunar = function(sy, sm, sd) {
sm -= 1;
let daySpan = (Date.UTC(sy, sm, sd) - Date.UTC(1949, 0, 29)) / (24 * 60 * 60 * 1e3) + 1;
let ly = 0, lm = 0, ld = 0, lz = "";
for (let j = 0; j < calendar.lunarInfo.length; j++) {
daySpan -= lunarYearDays(calendar.lunarInfo[j]);
if (daySpan <= 0) {
ly = 1949 + j;
daySpan += lunarYearDays(calendar.lunarInfo[j]);
break;
}
}
for (let k = 0; k < lunarYearMonths(calendar.lunarInfo[ly - 1949]).length; k++) {
daySpan -= lunarYearMonths(calendar.lunarInfo[ly - 1949])[k];
if (daySpan <= 0) {
const leapMonth = hasLeapMonth(calendar.lunarInfo[ly - 1949]);
if (leapMonth && leapMonth <= k) {
if (leapMonth < k) {
lm = k;
} else if (hasLeapMonth(calendar.lunarInfo[ly - 1949]) === k) {
lm = "闰" + k;
} else {
lm = k + 1;
}
} else {
lm = k + 1;
}
daySpan += lunarYearMonths(calendar.lunarInfo[ly - 1949])[k];
break;
}
}
ld = daySpan;
if (hasLeapMonth(calendar.lunarInfo[ly - 1949]) && (typeof lm === "string" && lm.indexOf("闰") > -1)) {
const num = /\d/.exec(lm);
lm = `闰${lunarMonth[+(num || 1) - 1]}`;
} else {
lm = lunarMonth[+(lm || 1) - 1];
}
ly = getTianGan(ly) + getDiZhi(ly);
if (ld < 11) {
ld = `${lunarDay[10]}${lunarDay[ld - 1]}`;
} else if (ld > 10 && ld < 20) {
ld = `${lunarDay[9]}${lunarDay[ld - 11]}`;
} else if (ld === 20) {
ld = `${lunarDay[1]}${lunarDay[9]}`;
} else if (ld > 20 && ld < 30) {
ld = `${lunarDay[11]}${lunarDay[ld - 21]}`;
} else if (ld === 30) {
ld = `${lunarDay[2]}${lunarDay[9]}`;
}
return {
lunarYear: ly,
lunarMonth: lm,
lunarDay: ld,
lunarShengXiao: lz
};
};
const getYearMonthDay = function(sy, sm, sd) {
sm -= 1;
let daySpan = (Date.UTC(sy, sm, sd) - Date.UTC(1949, 0, 29)) / (24 * 60 * 60 * 1e3) + 1;
let ly = 0, lm = 0, ld = 0;
for (let j = 0; j < calendar.lunarInfo.length; j++) {
daySpan -= lunarYearDays(calendar.lunarInfo[j]);
if (daySpan <= 0) {
ly = 1949 + j;
daySpan += lunarYearDays(calendar.lunarInfo[j]);
break;
}
}
for (let k = 0; k < lunarYearMonths(calendar.lunarInfo[ly - 1949]).length; k++) {
daySpan -= lunarYearMonths(calendar.lunarInfo[ly - 1949])[k];
if (daySpan <= 0) {
const leapMonth1 = hasLeapMonth(calendar.lunarInfo[ly - 1949]);
if (leapMonth1 && leapMonth1 <= k) {
if (leapMonth1 < k) {
lm = k;
} else if (leapMonth1 === k) {
lm = "闰" + k;
} else {
lm = k + 1;
}
} else {
lm = k + 1;
}
daySpan += lunarYearMonths(calendar.lunarInfo[ly - 1949])[k];
break;
}
}
ld = daySpan;
if (hasLeapMonth(calendar.lunarInfo[ly - 1949]) && (typeof lm === "string" && lm.indexOf("闰") > -1)) {
lm = `闰${lunarMonth[+(/\d/.exec(lm) || 1) - 1]}`;
} else {
lm = lunarMonth[+(lm || 1) - 1];
}
ly = getTianGan(ly) + getDiZhi(ly);
if (ld < 11) {
ld = `${lunarDay[10]}${lunarDay[ld - 1]}`;
} else if (ld > 10 && ld < 20) {
ld = `${lunarDay[9]}${lunarDay[ld - 11]}`;
} else if (ld === 20) {
ld = `${lunarDay[1]}${lunarDay[9]}`;
} else if (ld > 20 && ld < 30) {
ld = `${lunarDay[11]}${lunarDay[ld - 21]}`;
} else if (ld === 30) {
ld = `${lunarDay[2]}${lunarDay[9]}`;
}
return "" + ld;
};
function hasLeapMonth(ly) {
if (ly & 15) {
return ly & 15;
} else {
return false;
}
}
function leapMonthDays(ly) {
if (hasLeapMonth(ly)) {
return ly & 983040 ? 30 : 29;
} else {
return 0;
}
}
function lunarYearDays(ly) {
let totalDays = 0;
for (let i = 32768; i > 8; i >>= 1) {
let monthDays = ly & i ? 30 : 29;
totalDays += monthDays;
}
if (hasLeapMonth(ly)) {
totalDays += leapMonthDays(ly);
}
return totalDays;
}
function lunarYearMonths(ly) {
let monthArr = [];
for (let i = 32768; i > 8; i >>= 1) {
monthArr.push(ly & i ? 30 : 29);
}
const leapMonth = hasLeapMonth(ly);
if (leapMonth) {
monthArr.splice(leapMonth, 0, leapMonthDays(ly));
}
return monthArr;
}
function getTianGan(ly) {
let tianGanKey = (ly - 3) % 10;
if (tianGanKey === 0) tianGanKey = 10;
return calendar.Gan[tianGanKey - 1];
}
function getDiZhi(ly) {
let diZhiKey = (ly - 3) % 12;
if (diZhiKey === 0) diZhiKey = 12;
return diZhi[diZhiKey - 1];
}
const getYearMonthDayNew = (date) => {
date = date || /* @__PURE__ */ new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let week = date.getDay();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
return {
year,
month,
day,
week,
hour,
minute,
second,
weekNew: getWeek(week),
time: formatDate(year, month, day)
};
};
const defaultLunar = {
"1-1": "春节",
"1-15": "元宵节",
"2-2": "龙头节",
"5-5": "端午节",
"7-7": "七夕节",
"7-15": "中元节",
"8-15": "中秋节",
"9-9": "重阳节",
"10-1": "寒衣节",
"10-15": "下元节",
"12-8": "腊八节",
"12-23": "小年"
};
const defaultGregorian = {
"1-1": "元旦",
"2-14": "情人节",
"3-8": "妇女节",
"3-12": "植树节",
"5-1": "劳动节",
"5-4": "青年节",
"6-1": "儿童节",
"7-1": "建党节",
"8-1": "建军节",
"9-10": "教师节",
"10-1": "国庆节",
"12-24": "平安夜",
"12-25": "圣诞节"
};
class Calendar {
constructor() {
}
/**
* 农历1900-2100的润大小信息表
* @Array Of Property
* @return Hex
*/
lunarInfo = [
19416,
19168,
42352,
21717,
53856,
55632,
91476,
22176,
39632,
21970,
//1900-1909
19168,
42422,
42192,
53840,
119381,
46400,
54944,
44450,
38320,
84343,
//1910-1919
18800,
42160,
46261,
27216,
27968,
109396,
11104,
38256,
21234,
18800,
//1920-1929
25958,
54432,
59984,
28309,
23248,
11104,
100067,
37600,
116951,
51536,
//1930-1939
54432,
120998,
46416,
22176,
107956,
9680,
37584,
53938,
43344,
46423,
//1940-1949
27808,
46416,
86869,
19872,
42416,
83315,
21168,
43432,
59728,
27296,
//1950-1959
44710,
43856,
19296,
43748,
42352,
21088,
62051,
55632,
23383,
22176,
//1960-1969
38608,
19925,
19152,
42192,
54484,
53840,
54616,
46400,
46752,
103846,
//1970-1979
38320,
18864,
43380,
42160,
45690,
27216,
27968,
44870,
43872,
38256,
//1980-1989
19189,
18800,
25776,
29859,
59984,
27480,
21952,
43872,
38613,
37600,
//1990-1999
51552,
55636,
54432,
55888,
30034,
22176,
43959,
9680,
37584,
51893,
//2000-2009
43344,
46240,
47780,
44368,
21977,
19360,
42416,
86390,
21168,
43312,
//2010-2019
31060,
27296,
44368,
23378,
19296,
42726,
42208,
53856,
60005,
54576,
//2020-2029
23200,
30371,
38608,
19195,
19152,
42192,
118966,
53840,
54560,
56645,
//2030-2039
46496,
22224,
21938,
18864,
42359,
42160,
43600,
111189,
27936,
44448,
//2040-2049
/**Add By JJonline@JJonline.Cn**/
84835,
37744,
18936,
18800,
25776,
92326,
59984,
27424,
108228,
43744,
//2050-2059
41696,
53987,
51552,
54615,
54432,
55888,
23893,
22176,
42704,
21972,
//2060-2069
21200,
43448,
43344,
46240,
46758,
44368,
21920,
43940,
42416,
21168,
//2070-2079
45683,
26928,
29495,
27296,
44368,
84821,
19296,
42352,
21732,
53600,
//2080-2089
59752,
54560,
55968,
92838,
22224,
19168,
43476,
41680,
53584,
62034,
//2090-2099
54560
];
//2100
/**
* 公历每个月份的天数普通表
* @Array Of Property
* @return Number
*/
solarMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
/**
* 天干地支之天干速查表
* @Array Of Property trans["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"]
* @return Cn string
*/
Gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
/**
* 天干地支之地支速查表
* @Array Of Property
* @trans["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"]
* @return Cn string
*/
Zhi = [
"子",
"丑",
"寅",
"卯",
"辰",
"巳",
"午",
"未",
"申",
"酉",
"戌",
"亥"
];
/**
* 天干地支之地支速查表<=>生肖
* @Array Of Property
* @trans["鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"]
* @return Cn string
*/
Animals = [
"鼠",
"牛",
"虎",
"兔",
"龙",
"蛇",
"马",
"羊",
"猴",
"鸡",
"狗",
"猪"
];
/**
* 24节气速查表
* @Array Of Property
* @trans["小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至"]
* @return Cn string
*/
solarTerm = [
"小寒",
"大寒",
"立春",
"雨水",
"惊蛰",
"春分",
"清明",
"谷雨",
"立夏",
"小满",
"芒种",
"夏至",
"小暑",
"大暑",
"立秋",
"处暑",
"白露",
"秋分",
"寒露",
"霜降",
"立冬",
"小雪",
"大雪",
"冬至"
];
/**
* 1900-2100各年的24节气日期速查表
* @Array Of Property
* @return 0x string For splice
*/
sTermInfo = [
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c3598082c95f8c965cc920f",
"97bd0b06bdb0722c965ce1cfcc920f",
"b027097bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c359801ec95f8c965cc920f",
"97bd0b06bdb0722c965ce1cfcc920f",
"b027097bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c359801ec95f8c965cc920f",
"97bd0b06bdb0722c965ce1cfcc920f",
"b027097bd097c36b0b6fc9274c91aa",
"9778397bd19801ec9210c965cc920e",
"97b6b97bd19801ec95f8c965cc920f",
"97bd09801d98082c95f8e1cfcc920f",
"97bd097bd097c36b0b6fc9210c8dc2",
"9778397bd197c36c9210c9274c91aa",
"97b6b97bd19801ec95f8c965cc920e",
"97bd09801d98082c95f8e1cfcc920f",
"97bd097bd097c36b0b6fc9210c8dc2",
"9778397bd097c36c9210c9274c91aa",
"97b6b97bd19801ec95f8c965cc920e",
"97bcf97c3598082c95f8e1cfcc920f",
"97bd097bd097c36b0b6fc9210c8dc2",
"9778397bd097c36c9210c9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c3598082c95f8c965cc920f",
"97bd097bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c3598082c95f8c965cc920f",
"97bd097bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c359801ec95f8c965cc920f",
"97bd097bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c359801ec95f8c965cc920f",
"97bd097bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf97c359801ec95f8c965cc920f",
"97bd097bd07f595b0b6fc920fb0722",
"9778397bd097c36b0b6fc9210c8dc2",
"9778397bd19801ec9210c9274c920e",
"97b6b97bd19801ec95f8c965cc920f",
"97bd07f5307f595b0b0bc920fb0722",
"7f0e397bd097c36b0b6fc9210c8dc2",
"9778397bd097c36c9210c9274c920e",
"97b6b97bd19801ec95f8c965cc920f",
"97bd07f5307f595b0b0bc920fb0722",
"7f0e397bd097c36b0b6fc9210c8dc2",
"9778397bd097c36c9210c9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bd07f1487f595b0b0bc920fb0722",
"7f0e397bd097c36b0b6fc9210c8dc2",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf7f1487f595b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf7f1487f595b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf7f1487f531b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c965cc920e",
"97bcf7f1487f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b97bd19801ec9210c9274c920e",
"97bcf7f0e47f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"9778397bd097c36b0b6fc9210c91aa",
"97b6b97bd197c36c9210c9274c920e",
"97bcf7f0e47f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"9778397bd097c36b0b6fc9210c8dc2",
"9778397bd097c36c9210c9274c920e",
"97b6b7f0e47f531b0723b0b6fb0722",
"7f0e37f5307f595b0b0bc920fb0722",
"7f0e397bd097c36b0b6fc9210c8dc2",
"9778397bd097c36b0b70c9274c91aa",
"97b6b7f0e47f531b0723b0b6fb0721",
"7f0e37f1487f595b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc9210c8dc2",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f595b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"9778397bd097c36b0b6fc9274c91aa",
"97b6b7f0e47f531b0723b0787b0721",
"7f0e27f0e47f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"9778397bd097c36b0b6fc9210c91aa",
"97b6b7f0e47f149b0723b0787b0721",
"7f0e27f0e47f531b0723b0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"9778397bd097c36b0b6fc9210c8dc2",
"977837f0e37f149b0723b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0722",
"7f0e37f5307f595b0b0bc920fb0722",
"7f0e397bd097c35b0b6fc9210c8dc2",
"977837f0e37f14998082b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e37f1487f595b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc9210c8dc2",
"977837f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"977837f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd097c35b0b6fc920fb0722",
"977837f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"977837f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"977837f0e37f14998082b0787b06bd",
"7f07e7f0e47f149b0723b0787b0721",
"7f0e27f0e47f531b0b0bb0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"977837f0e37f14998082b0723b06bd",
"7f07e7f0e37f149b0723b0787b0721",
"7f0e27f0e47f531b0723b0b6fb0722",
"7f0e397bd07f595b0b0bc920fb0722",
"977837f0e37f14898082b0723b02d5",
"7ec967f0e37f14998082b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0722",
"7f0e37f1487f595b0b0bb0b6fb0722",
"7f0e37f0e37f14898082b0723b02d5",
"7ec967f0e37f14998082b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0722",
"7f0e37f1487f531b0b0bb0b6fb0722",
"7f0e37f0e37f14898082b0723b02d5",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e37f1487f531b0b0bb0b6fb0722",
"7f0e37f0e37f14898082b072297c35",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e37f0e37f14898082b072297c35",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e37f0e366aa89801eb072297c35",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f149b0723b0787b0721",
"7f0e27f1487f531b0b0bb0b6fb0722",
"7f0e37f0e366aa89801eb072297c35",
"7ec967f0e37f14998082b0723b06bd",
"7f07e7f0e47f149b0723b0787b0721",
"7f0e27f0e47f531b0723b0b6fb0722",
"7f0e37f0e366aa89801eb072297c35",
"7ec967f0e37f14998082b0723b06bd",
"7f07e7f0e37f14998083b0787b0721",
"7f0e27f0e47f531b0723b0b6fb0722",
"7f0e37f0e366aa89801eb072297c35",
"7ec967f0e37f14898082b0723b02d5",
"7f07e7f0e37f14998082b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0722",
"7f0e36665b66aa89801e9808297c35",
"665f67f0e37f14898082b0723b02d5",
"7ec967f0e37f14998082b0787b0721",
"7f07e7f0e47f531b0723b0b6fb0722",
"7f0e36665b66a449801e9808297c35",
"665f67f0e37f14898082b0723b02d5",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e36665b66a449801e9808297c35",
"665f67f0e37f14898082b072297c35",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e26665b66a449801e9808297c35",
"665f67f0e37f1489801eb072297c35",
"7ec967f0e37f14998082b0787b06bd",
"7f07e7f0e47f531b0723b0b6fb0721",
"7f0e27f1487f531b0b0bb0b6fb0722"
];
/**
* 数字转中文速查表
* @Array Of Property
* @trans ['日','一','二','三','四','五','六','七','八','九','十']
* @return Cn string
*/
nStr1 = [
"日",
"一",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九",
"十"
];
/**
* 日期转农历称呼速查表
* @Array Of Property
* @trans ['初','十','廿','卅']
* @return Cn string
*/
nStr2 = ["初", "十", "廿", "卅"];
/**
* 月份转农历称呼速查表
* @Array Of Property
* @trans ['正','一','二','三','四','五','六','七','八','九','十','冬','腊']
* @return Cn string
*/
nStr3 = [
"正",
"二",
"三",
"四",
"五",
"六",
"七",
"八",
"九",
"十",
"冬",
"腊"
];
/**
* 返回农历y年一整年的总天数
* @param lunar Year
* @return Number
* @eg:var count = calendar.lYearDays(1987) ;//count=387
*/
lYearDays(y) {
var i, sum = 348;
for (i = 32768; i > 8; i >>= 1) {
sum += calendar.lunarInfo[y - 1900] & i ? 1 : 0;
}
return sum + calendar.leapDays(y);
}
/**
* 返回农历y年闰月是哪个月;若y年没有闰月 则返回0
* @param lunar Year
* @return Number (0-12)
* @eg:var leapMonth = calendar.leapMonth(1987) ;//leapMonth=6
*/
leapMonth(y) {
return calendar.lunarInfo[y - 1900] & 15;
}
/**
* 返回农历y年闰月的天数 若该年没有闰月则返回0
* @param lunar Year
* @return Number (0、29、30)
* @eg:var leapMonthDay = calendar.leapDays(1987) ;//leapMonthDay=29
*/
leapDays(y) {
if (calendar.leapMonth(y)) {
return calendar.lunarInfo[y - 1900] & 65536 ? 30 : 29;
}
return 0;
}
/**
* 返回农历y年m月(非闰月)的总天数,计算m为闰月时的天数请使用leapDays方法
* @param lunar Year
* @return Number (-1、29、30)
* @eg:var MonthDay = calendar.monthDays(1987,9) ;//MonthDay=29
*/
monthDays(y, m) {
if (m > 12 || m < 1) {
return -1;
}
return calendar.lunarInfo[y - 1900] & 65536 >> m ? 30 : 29;
}
/**
* 返回公历(!)y年m月的天数
* @param solar Year
* @return Number (-1、28、29、30、31)
* @eg:var solarMonthDay = calendar.leapDays(1987) ;//solarMonthDay=30
*/
solarDays(y, m) {
if (m > 12 || m < 1) {
return -1;
}
var ms = m - 1;
if (ms == 1) {
return y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28;
} else {
return calendar.solarMonth[ms];
}
}
/**
* 农历年份转换为干支纪年
* @param lYear 农历年的年份数
* @return Cn string
*/
toGanZhiYear(lYear) {
var ganKey = (lYear - 3) % 10;
var zhiKey = (lYear - 3) % 12;
if (ganKey == 0) ganKey = 10;
if (zhiKey == 0) zhiKey = 12;
return calendar.Gan[ganKey - 1] + calendar.Zhi[zhiKey - 1];
}
/**
* 公历月、日判断所属星座
* @param cMonth [description]
* @param cDay [description]
* @return Cn string
*/
toAstro(cMonth, cDay) {
var s = "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
var arr = [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22];
return s.substr(cMonth * 2 - (cDay < arr[cMonth - 1] ? 2 : 0), 2) + "座";
}
/**
* 传入offset偏移量返回干支
* @param offset 相对甲子的偏移量
* @return Cn string
*/
toGanZhi(offset) {
return calendar.Gan[offset % 10] + calendar.Zhi[offset % 12];
}
/**
* 传入公历(!)y年获得该年第n个节气的公历日期
* @param y公历年(1900-2100);n二十四节气中的第几个节气(1~24);从n=1(小寒)算起
* @return day Number
* @eg:var _24 = calendar.getTerm(1987,3) ;//_24=4;意即1987年2月4日立春
*/
getTerm(y, n) {
if (y < 1900 || y > 2100) {
return -1;
}
if (n < 1 || n > 24) {
return -1;
}
var _table = calendar.sTermInfo[y - 1900];
var _info = [
parseInt("0x" + _table.substr(0, 5)).toString(),
parseInt("0x" + _table.substr(5, 5)).toString(),
parseInt("0x" + _table.substr(10, 5)).toString(),
parseInt("0x" + _table.substr(15, 5)).toString(),
parseInt("0x" + _table.substr(20, 5)).toString(),
parseInt("0x" + _table.substr(25, 5)).toString()
];
var _calday = [
_info[0].substr(0, 1),
_info[0].substr(1, 2),
_info[0].substr(3, 1),
_info[0].substr(4, 2),
_info[1].substr(0, 1),
_info[1].substr(1, 2),
_info[1].substr(3, 1),
_info[1].substr(4, 2),
_info[2].substr(0, 1),
_info[2].substr(1, 2),
_info[2].substr(3, 1),
_info[2].substr(4, 2),
_info[3].substr(0, 1),
_info[3].substr(1, 2),
_info[3].substr(3, 1),
_info[3].substr(4, 2),
_info[4].substr(0, 1),
_info[4].substr(1, 2),
_info[4].substr(3, 1),
_info[4].substr(4, 2),
_info[5].substr(0, 1),
_info[5].substr(1, 2),
_info[5].substr(3, 1),
_info[5].substr(4, 2)
];
return parseInt(_calday[n - 1]);
}
/**
* 传入农历数字月份返回汉语通俗表示法
* @param lunar month
* @return Cn string
* @eg:var cnMonth = calendar.toChinaMonth(12) ;//cnMonth='腊月'
*/
toChinaMonth(m) {
if (m > 12 || m < 1) {
return -1;
}
var s = calendar.nStr3[m - 1];
s += "月";
return s;
}
/**
* 传入农历日期数字返回汉字表示法
* @param lunar day
* @return Cn string
* @eg:var cnDay = calendar.toChinaDay(21) ;//cnMonth='廿一'
*/
toChinaDay(d) {
var s;
switch (d) {
case 10:
s = "初十";
break;
case 20:
s = "二十";
break;
case 30:
s = "三十";
break;
default:
s = calendar.nStr2[Math.floor(d / 10)];
s += calendar.nStr1[d % 10];
}
return s;
}
/**
* 年份转生肖[!仅能大致转换] => 精确划分生肖分界线是“立春”
* @param y year
* @return Cn string
* @eg:var animal = calendar.getAnimal(1987) ;//animal='兔'
*/
getAnimal(y) {
return calendar.Animals[(y - 4) % 12];
}
/**
* 传入阳历年月日获得详细的公历、农历object信息 <=>JSON
* @param y solar year
* @param m solar month
* @param d solar day
* @return JSON object
* @eg:console.log(calendar.solar2lunar(1987,11,01));
*/
solar2lunar(y, m, d) {
if (y < 1900 || y > 2100) {
return -1;
}
if (y == 1900 && m == 1 && d < 31) {
return -1;
}
if (!y) {
var objDate = /* @__PURE__ */ new Date();
} else {
var objDate = new Date(y, parseInt("" + m) - 1, d);
}
var i, leap = 0, temp = 0;
var y = objDate.getFullYear(), m = objDate.getMonth() + 1, d = objDate.getDate();
var offset = (Date.UTC(objDate.getFullYear(), objDate.getMonth(), objDate.getDate()) - Date.UTC(
1900,
0,
31
)) / 864e5;
for (i = 1900; i < 2101 && offset > 0; i++) {
temp = calendar.lYearDays(i);
offset -= temp;
}
if (offset < 0) {
offset += temp;
i--;
}
var isTodayObj = /* @__PURE__ */ new Date(), isToday = false;
if (isTodayObj.getFullYear() == y && isTodayObj.getMonth() + 1 == m && isTodayObj.getDate() == d) {
isToday = true;
}
var nWeek = objDate.getDay(), cWeek = calendar.nStr1[nWeek];
if (nWeek == 0) {
nWeek = 7;
}
var year = i;
var leap = calendar.leapMonth(i);
var isLeap = false;
for (i = 1; i < 13 && offset > 0; i++) {
if (leap > 0 && i == leap + 1 && isLeap == false) {
--i;
isLeap = true;
temp = calendar.leapDays(year);
} else {
temp = calendar.monthDays(year, i);
}
if (isLeap == true && i == leap + 1) {
isLeap = false;
}
offset -= temp;
}
if (offset == 0 && leap > 0 && i == leap + 1)
if (isLeap) {
isLeap = false;
} else {
isLeap = true;
--i;
}
if (offset < 0) {
offset += temp;
--i;
}
var month = i;
var day = offset + 1;
var sm = m - 1;
var gzY = calendar.toGanZhiYear(year);
var firstNode = calendar.getTerm(year, m * 2 - 1);
var secondNode = calendar.getTerm(year, m * 2);
var gzM = calendar.toGanZhi((y - 1900) * 12 + m + 11);
if (d >= firstNode) {
gzM = calendar.toGanZhi((y - 1900) * 12 + m + 12);
}
var isTerm = false;
var Term = null;
if (firstNode == d) {
isTerm = true;
Term = calendar.solarTerm[m * 2 - 2];
}
if (secondNode == d) {
isTerm = true;
Term = calendar.solarTerm[m * 2 - 1];
}
var dayCyclical = Date.UTC(y, sm, 1, 0, 0, 0, 0) / 864e5 + 25567 + 10;
var gzD = calendar.toGanZhi(dayCyclical + d - 1);
var astro = calendar.toAstro(m, d);
return {
"gzYear": gzY,
"gzMonth": gzM,
"gzDay": gzD,
"lYear": year,
"lMonth": month,
"lDay": day,
"animal": calendar.getAnimal(year),
"monthCn": (isLeap ? "闰" : "") + calendar.toChinaMonth(month),
"dayCn": calendar.toChinaDay(day),
"year": y,
"month": m,
"day": d,
"isToday": isToday,
"isLeap": isLeap,
"isTerm": isTerm,
"week": nWeek,
"weekCn": "星期" + cWeek,
"term": Term,
"astro": astro
};
}
/**
* 传入农历年月日以及传入的月份是否闰月获得详细的公历、农历object信息 <=>JSON
* @param y lunar year
* @param m lunar month
* @param d lunar day
* @param isLeapMonth lunar month is leap or not.[如果是农历闰月第四个参数赋值true即可]
* @return JSON object
* @eg:console.log(calendar.lunar2solar(1987,9,10));
*/
lunar2solar(y, m, d, isLeapMonth) {
isLeapMonth = !!isLeapMonth;
let leapMonth = calendar.leapMonth(y);
calendar.leapDays(y);
if (isLeapMonth && leapMonth != m) {
return -1;
}
if (y == 2100 && m == 12 && d > 1 || y == 1900 && m == 1 && d < 31) {
return -1;
}
let day = calendar.monthDays(y, m);
let _day = day;
if (isLeapMonth) {
_day = calendar.leapDays(y);
}
if (y < 1900 || y > 2100 || d > _day) {
return -1;
}
let offset = 0;
for (let i = 1900; i < y; i++) {
offset += calendar.lYearDays(i);
}
let leap = 0, isAdd = false;
for (let i = 1; i < m; i++) {
leap = calendar.leapMonth(y);
if (!isAdd) {
if (leap <= i && leap > 0) {
offset += calendar.leapDays(y);
isAdd = true;
}
}
offset += calendar.monthDays(y, i);
}
if (isLeapMonth) {
offset += day;
}
var stmap = Date.UTC(1900, 1, 30, 0, 0, 0);
var calObj = new Date((offset + d - 31) * 864e5 + stmap);
var cY = calObj.getUTCFullYear();
var cM = calObj.getUTCMonth() + 1;
var cD = calObj.getUTCDate();
return calendar.solar2lunar(cY, cM, cD);
}
/**
* @desc 获取时间
* @param { number } day -7 一周前 -1 昨天 0 今天 1 明天 7 一周后
* @param { DateValue } data new Date()
*/
$getDay(day, data) {
let days;
if (data) {
days = new Date(data);
} else {
days = /* @__PURE__ */ new Date();
}
let gettimes = days.getTime() + 1e3 * 60 * 60 * 24 * day;
days.setTime(gettimes);
let year = days.getFullYear();
let month = days.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
let today = days.getDate();
if (today < 10) {
today = "0" + today;
}
return year + "/" + month + "/" + today;
}
}
var calendar = new Calendar();
const CalendarInstance = calendar;
export { Calendar, CalendarInstance, defaultGregorian, defaultLunar, getYearMonthDay, getYearMonthDayNew, sloarToLunar };