lunar-typescript-pro
Version:
lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)、佛历和道历工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋、凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶等。lunar is a calendar library for Solar and Chinese Lunar.
92 lines (91 loc) • 2.5 kB
JavaScript
import { LunarUtil } from "./LunarUtil.mjs";
import { DaYun } from "./DaYun.mjs";
export class Yun {
_gender;
_startYear;
_startMonth;
_startDay;
_startHour;
_forward;
_lunar;
constructor(lunar, gender, sect = 1) {
this._gender = gender;
this._lunar = lunar;
const yang = 0 === lunar.getYearGanIndexExact() % 2;
const man = 1 === gender;
const forward = yang && man || !yang && !man;
this._forward = forward;
const prev = lunar.getPrevJie();
const next = lunar.getNextJie();
const current = lunar.getSolar();
const start = forward ? current : prev.getSolar();
const end = forward ? next.getSolar() : current;
let year;
let month;
let day;
let hour = 0;
if (2 === sect) {
let minutes = end.subtractMinute(start);
year = Math.floor(minutes / 4320);
minutes -= year * 4320;
month = Math.floor(minutes / 360);
minutes -= month * 360;
day = Math.floor(minutes / 12);
minutes -= day * 12;
hour = minutes * 2;
} else {
const endTimeZhiIndex = end.getHour() == 23 ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substring(11, 16));
const startTimeZhiIndex = start.getHour() == 23 ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substring(11, 16));
let hourDiff = endTimeZhiIndex - startTimeZhiIndex;
let dayDiff = end.subtract(start);
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;
}
let monthDiff = Math.floor(hourDiff * 10 / 30);
month = dayDiff * 4 + monthDiff;
day = hourDiff * 10 - monthDiff * 30;
year = Math.floor(month / 12);
month = month - year * 12;
}
this._startYear = year;
this._startMonth = month;
this._startDay = day;
this._startHour = hour;
}
getGender() {
return this._gender;
}
getStartYear() {
return this._startYear;
}
getStartMonth() {
return this._startMonth;
}
getStartDay() {
return this._startDay;
}
getStartHour() {
return this._startHour;
}
isForward() {
return this._forward;
}
getLunar() {
return this._lunar;
}
getStartSolar() {
let solar = this._lunar.getSolar();
solar = solar.nextYear(this._startYear);
solar = solar.nextMonth(this._startMonth);
solar = solar.next(this._startDay);
return solar.nextHour(this._startHour);
}
getDaYun(n = 10) {
const l = [];
for (let i = 0; i < n; i++) {
l.push(new DaYun(this, i));
}
return l;
}
}