@z-yue/calendar
Version:
根据当前阳(公)历日期获取阴(农)历日期,节日
125 lines (90 loc) • 3.47 kB
JavaScript
const lunarMonth = ['', '正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '冬月', '腊月'];
const lunarDate = ['', '初一', '初二', '初三', '初四', '初五', '初六', '初七', '初八', '初九', '初十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十', '廿一', '廿二', '廿三', '廿四', '廿五', '廿六', '廿七', '廿八', '廿九', '三十'];
class Lunar {
constructor(date = new Date()) {
this.LOCALES = 'ja-JP-u-ca-chinese';
this.match = (month, date) => this.month === month && this.date === date;
this.isNewYear = () => this.match(1, 1) || this.match(1, 2) || this.match(1, 3) || this.match(1, 4) || this.match(1, 5) || this.match(1, 6) || this.match(1, 7);
this.isLantern = () => this.match(1, 15);
this.isDoubleSeventh = () => this.match(7, 7);
this.isMidAutumn = () => this.match(8, 15);
this.isDoubleNinth = () => this.match(9, 9);
this.now = date.toLocaleString(this.LOCALES);
this.publicDate = date;
}
get year() {
return this.now.slice(0, this.now.search(/\d+月/));
}
get month() {
return parseInt(this.now.slice(this.now.search(/\d+月/), this.now.search(/月/)));
}
get date() {
return parseInt(this.now.slice(this.now.search(/\d+日/), this.now.search(/日/)));
}
get lunarMonth() {
return lunarMonth[this.month] || '';
}
get lunarDate() {
return lunarDate[this.date] || '';
}
get isBigMonth() {
const diff = 30 - this.date;
const publicDate = new Date(this.publicDate);
publicDate.setDate(publicDate.getDate() + diff);
const newDate = publicDate.toLocaleString('ja-JP-u-ca-chinese');
const newMonth = parseInt(newDate.slice(newDate.search(/\d+月/), newDate.search(/月/)));
return newMonth === this.month;
}
get isSmallMonth() {
return !this.isBigMonth;
}
get full() {
return `${this.year}${this.lunarMonth}${this.lunarDate}`;
}
get festival() {
if (this.isNewYear()) return '新年';
if (this.isLantern()) return '元宵节';
if (this.isDoubleSeventh()) return '七夕节';
if (this.isMidAutumn()) return '中秋节';
if (this.isDoubleNinth()) return '重阳节';
return '';
}
}
class Calendar {
constructor(date = new Date()) {
this.match = (month, date) => this.month === month && this.date === date;
this.isTombSweeping = () => this.match(4, 4);
this.isValentineDay = () => this.match(2, 14);
this.isAprilFoolDay = () => this.match(4, 1);
this.isChristmas = () => this.match(12, 25);
this.isHalloween = () => this.match(10, 31);
this.isNewYear = () => this.match(1, 1);
this.now = date;
this.lunar = new Lunar(date);
}
get year() {
return this.now.getFullYear();
}
get month() {
return this.now.getMonth() + 1;
}
get date() {
return this.now.getDate();
}
get day() {
return this.now.getDay();
}
get festival() {
if (this.isTombSweeping()) return '清明节';
if (this.isValentineDay()) return '情人节';
if (this.isAprilFoolDay()) return '愚人节';
if (this.isChristmas()) return '圣诞节';
if (this.isHalloween()) return '万圣节';
if (this.isNewYear()) return '元旦节';
return '';
}
get fullFestival() {
return [this.festival, this.lunar.festival].filter(f => f !== '').join(' ');
}
}
export default Calendar;