lunar-typescript
Version:
lunar是一款无第三方依赖的公历(阳历)、农历(阴历、老黄历)、佛历和道历工具,支持星座、儒略日、干支、生肖、节气、节日、彭祖百忌、每日宜忌、吉神宜趋、凶煞宜忌、吉神(喜神/福神/财神/阳贵神/阴贵神)方位、胎神方位、冲煞、纳音、星宿、八字、五行、十神、建除十二值星、青龙名堂等十二神、黄道日及吉凶等。lunar is a calendar library for Solar and Chinese Lunar.
65 lines (64 loc) • 1.57 kB
JavaScript
import { SolarUtil } from "./SolarUtil.mjs";
import { Solar } from "./Solar.mjs";
import { SolarWeek } from "./SolarWeek.mjs";
export class SolarMonth {
static fromYm(year, month) {
return new SolarMonth(year, month);
}
static fromDate(date) {
return SolarMonth.fromYm(date.getFullYear(), date.getMonth() + 1);
}
constructor(year, month) {
this._year = year;
this._month = month;
}
getYear() {
return this._year;
}
getMonth() {
return this._month;
}
next(months) {
const n = months < 0 ? -1 : 1;
let m = Math.abs(months);
let y = this._year + Math.floor(m / 12) * n;
m = this._month + m % 12 * n;
if (m > 12) {
m -= 12;
y++;
} else if (m < 1) {
m += 12;
y--;
}
return SolarMonth.fromYm(y, m);
}
getDays() {
const l = [];
const d = Solar.fromYmd(this._year, this._month, 1);
l.push(d);
const days = SolarUtil.getDaysOfMonth(this._year, this._month);
for (let i = 1; i < days; i++) {
l.push(d.next(i));
}
return l;
}
getWeeks(start) {
const l = [];
let week = SolarWeek.fromYmd(this._year, this._month, 1, start);
while (true) {
l.push(week);
week = week.next(1, false);
const firstDay = week.getFirstDay();
if (firstDay.getYear() > this._year || firstDay.getMonth() > this._month) {
break;
}
}
return l;
}
toString() {
return `${this.getYear()}-${this.getMonth()}`;
}
toFullString() {
return `${this.getYear()}\u5E74${this.getMonth()}\u6708`;
}
}