1
0
mirror of synced 2026-04-16 21:38:40 +08:00

Compare commits

...

3 Commits

6 changed files with 154 additions and 25 deletions

View File

@@ -570,3 +570,23 @@ test('test052', () => {
const lunar = Solar.fromYmd(2011, 11, 12).getLunar();
expect(lunar.getDayPositionTai()).toBe('厕灶厨 外西南');
});
test('test053', () => {
const lunar = Solar.fromYmd(1722, 9, 25).getLunar();
expect(lunar.getOtherFestivals() + '').toBe('秋社');
});
test('test054', () => {
const lunar = Solar.fromYmd(840, 9, 14).getLunar();
expect(lunar.getOtherFestivals() + '').toBe('秋社');
});
test('test055', () => {
const lunar = Solar.fromYmd(2022, 3, 16).getLunar();
expect(lunar.getOtherFestivals() + '').toBe('春社');
});
test('test056', () => {
const lunar = Solar.fromYmd(2021, 3, 21).getLunar();
expect(lunar.getOtherFestivals() + '').toBe('春社');
});

View File

@@ -31,3 +31,18 @@ test('9', () => {
test('10', () => {
expect(SolarUtil.isLeapYear(1500)).toBe(false);
});
test('11', () => {
var solar = Solar.fromYmd(2022, 3, 28);
expect(solar.getFestivals() + '').toBe('全国中小学生安全教育日');
});
test('12', () => {
var solar = Solar.fromYmd(2021, 3, 29);
expect(solar.getFestivals() + '').toBe('全国中小学生安全教育日');
});
test('13', () => {
var solar = Solar.fromYmd(1996, 3, 25);
expect(solar.getFestivals() + '').toBe('全国中小学生安全教育日');
});

View File

@@ -31,3 +31,9 @@ test('test2', () => {
const week = SolarWeek.fromYmd(2021, 5, 4, start);
expect(week.getIndex()).toBe(2);
});
test('test3', () => {
const start = 0;
const week = SolarWeek.fromYmd(2022, 3, 6, start);
expect(week.getIndexInYear()).toBe(11);
});

View File

@@ -31,3 +31,30 @@ it('test3', () => {
expect(yun.getStartDay()).toBe(0);
expect(yun.getStartSolar().toYmd()).toBe('2020-02-06');
});
it('test4', () => {
const solar = Solar.fromYmdHms(2022, 3, 9, 20, 51, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
const yun = eightChar.getYun(1);
expect(yun.getStartSolar().toYmd()).toBe('2030-12-19');
});
it('test5', () => {
const solar = Solar.fromYmdHms(2022, 3, 9, 20, 51, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
const yun = eightChar.getYun(1, 2);
expect(yun.getStartYear()).toBe(8);
expect(yun.getStartMonth()).toBe(9);
expect(yun.getStartDay()).toBe(2);
expect(yun.getStartSolar().toYmd()).toBe('2030-12-12');
});
it('test6', () => {
const solar = Solar.fromYmdHms(2018, 6, 11, 9, 30, 0);
const lunar = solar.getLunar();
const eightChar = lunar.getEightChar();
const yun = eightChar.getYun(0, 2);
expect(yun.getStartSolar().toYmd()).toBe('2020-03-21');
});

109
lunar.js
View File

@@ -163,6 +163,12 @@
if(f){
l.push(f);
}
if (this._p.day + 7 >= SolarUtil.getDaysOfMonth(this._p.year, this._p.month)) {
f = SolarUtil.WEEK_FESTIVAL[this._p.month + '-0-' + week];
if (f) {
l.push(f);
}
}
return l;
},
getOtherFestivals:function(){
@@ -1073,9 +1079,28 @@
if(fs){
l=l.concat(fs);
}
if(this._p.solar.toYmd()===this._p.jieQi['清明'].next(-1).toYmd()){
var solarYmd = this._p.solar.toYmd();
if(this._p.solar.toYmd() === this._p.jieQi['清明'].next(-1).toYmd()){
l.push('寒食节');
}
var jq = this._p.jieQi['立春'];
var offset = 4 - jq.getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10
}
if (solarYmd === jq.next(offset + 40).toYmd()) {
l.push('春社');
}
jq = this._p.jieQi['立秋'];
offset = 4 - jq.getLunar().getDayGanIndex();
if (offset < 0) {
offset += 10
}
if (solarYmd === jq.next(offset + 40).toYmd()) {
l.push('秋社');
}
return l;
},
getBaZi:function(){
@@ -1174,11 +1199,11 @@
}
return LunarUtil.POSITION_TAI_MONTH[m-1];
},
getDayYi:function(){
return LunarUtil.getDayYi(this.getMonthInGanZhiExact(),this.getDayInGanZhi());
getDayYi:function(sect){
return LunarUtil.getDayYi(2 == sect ? this.getMonthInGanZhiExact() : this.getMonthInGanZhi(), this.getDayInGanZhi());
},
getDayJi:function(){
return LunarUtil.getDayJi(this.getMonthInGanZhiExact(),this.getDayInGanZhi());
getDayJi:function(sect){
return LunarUtil.getDayJi(2 == sect ? this.getMonthInGanZhiExact() : this.getMonthInGanZhi(), this.getDayInGanZhi());
},
getDayJiShen:function(){
return LunarUtil.getDayJiShen(this.getMonth(),this.getDayInGanZhi());
@@ -1735,6 +1760,19 @@
}
return Math.ceil((this._p.day + offset)/7);
},
/**
* 获取当前日期是在当年第几周
* @return number 周序号从1开始
*/
getIndexInYear:function(){
var firstDate = ExactDate.fromYmd(this._p.year, 1, 1);
var firstDayWeek = firstDate.getDay();
var offset = firstDayWeek - this._p.start;
if(offset < 0) {
offset += 7;
}
return Math.ceil((SolarUtil.getDaysInYear(this._p.year, this._p.month, this._p.day) + offset)/7);
},
/**
* 周推移
* @param weeks 推移的周数,负数为倒推
@@ -2743,9 +2781,9 @@
WEEK:['日','一','二','三','四','五','六'],
DAYS_OF_MONTH:[31,28,31,30,31,30,31,31,30,31,30,31],
XINGZUO:['白羊','金牛','双子','巨蟹','狮子','处女','天秤','天蝎','射手','摩羯','水瓶','双鱼'],
FESTIVAL:{'1-1':'元旦节','2-14':'情人节','3-8':'妇女节','3-12':'植树节','3-15':'消费者权益日','4-1':'愚人节','5-1':'劳动节','5-4':'青年节','6-1':'儿童节','7-1':'建党节','8-1':'建军节','9-10':'教师节','10-1':'国庆节','12-24':'平安夜','12-25':'圣诞节'},
FESTIVAL:{'1-1':'元旦节','2-14':'情人节','3-8':'妇女节','3-12':'植树节','3-15':'消费者权益日','4-1':'愚人节','5-1':'劳动节','5-4':'青年节','6-1':'儿童节','7-1':'建党节','8-1':'建军节','9-10':'教师节','10-1':'国庆节','10-31':'万圣节前夜','11-1':'万圣节','12-24':'平安夜','12-25':'圣诞节'},
OTHER_FESTIVAL:{'1-8':['周恩来逝世纪念日'],'1-10':['中国人民警察节','中国公安110宣传日'],'1-21':['列宁逝世纪念日'],'1-26':['国际海关日'],'2-2':['世界湿地日'],'2-4':['世界抗癌日'],'2-7':['京汉铁路罢工纪念'],'2-10':['国际气象节'],'2-19':['邓小平逝世纪念日'],'2-21':['国际母语日'],'2-24':['第三世界青年日'],'3-1':['国际海豹日'],'3-3':['全国爱耳日'],'3-5':['周恩来诞辰纪念日','中国青年志愿者服务日'],'3-6':['世界青光眼日'],'3-12':['孙中山逝世纪念日'],'3-14':['马克思逝世纪念日'],'3-17':['国际航海日'],'3-18':['全国科技人才活动日'],'3-21':['世界森林日','世界睡眠日'],'3-22':['世界水日'],'3-23':['世界气象日'],'3-24':['世界防治结核病日'],'4-2':['国际儿童图书日'],'4-7':['世界卫生日'],'4-22':['列宁诞辰纪念日'],'4-23':['世界图书和版权日'],'4-26':['世界知识产权日'],'5-3':['世界新闻自由日'],'5-5':['马克思诞辰纪念日'],'5-8':['世界红十字日'],'5-11':['世界肥胖日'],'5-27':['上海解放日'],'5-31':['世界无烟日'],'6-5':['世界环境日'],'6-6':['全国爱眼日'],'6-8':['世界海洋日'],'6-11':['中国人口日'],'6-14':['世界献血日'],'7-1':['香港回归纪念日'],'7-7':['中国人民抗日战争纪念日'],'7-11':['世界人口日'],'8-5':['恩格斯逝世纪念日'],'8-6':['国际电影节'],'8-12':['国际青年日'],'8-22':['邓小平诞辰纪念日'],'9-3':['中国抗日战争胜利纪念日'],'9-8':['世界扫盲日'],'9-9':['毛泽东逝世纪念日'],'9-14':['世界清洁地球日'],'9-18':['九一八事变纪念日'],'9-20':['全国爱牙日'],'9-21':['国际和平日'],'9-27':['世界旅游日'],'10-4':['世界动物日'],'10-10':['辛亥革命纪念日'],'10-13':['中国少年先锋队诞辰日'],'10-25':['抗美援朝纪念日'],'11-12':['孙中山诞辰纪念日'],'11-17':['国际大学生节'],'11-28':['恩格斯诞辰纪念日'],'12-1':['世界艾滋病日'],'12-12':['西安事变纪念日'],'12-13':['国家公祭日'],'12-26':['毛泽东诞辰纪念日']},
WEEK_FESTIVAL:{'5-2-0':'母亲节','6-3-0':'父亲节','11-4-4':'感恩节'},
WEEK_FESTIVAL:{'3-0-1':'全国中小学生安全教育日','5-2-0':'母亲节','6-3-0':'父亲节','11-4-4':'感恩节'},
isLeapYear:function(year){return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);},
getDaysOfMonth:function(year,month){
var m = month-1;
@@ -2820,7 +2858,7 @@
GONG:{'角':'东','井':'南','奎':'西','斗':'北','亢':'东','鬼':'南','娄':'西','牛':'北','氐':'东','柳':'南','胃':'西','女':'北','房':'东','星':'南','昴':'西','虚':'北','心':'东','张':'南','毕':'西','危':'北','尾':'东','翼':'南','觜':'西','室':'北','箕':'东','轸':'南','参':'西','壁':'北' },
SHOU:{'东':'青龙','南':'朱雀','西':'白虎','北':'玄武'},
FESTIVAL:{'1-1':'春节','1-15':'元宵节','2-2':'龙头节','5-5':'端午节','7-7':'七夕节','8-15':'中秋节','9-9':'重阳节','12-8':'腊八节'},
OTHER_FESTIVAL:{'1-4':['接神日'],'1-5':['隔开日'],'1-7':['人日'],'1-8':['谷日','顺星节'],'1-9':['天日'],'1-10':['地日'],'1-20':['天穿节'],'1-25':['填仓节'],'1-30':['正月晦'],'2-1':['中和节'],'2-2':['社'],'3-3':['上巳节'],'5-20':['分龙节'],'5-25':['会龙节'],'6-6':['天贶节'],'6-24':['观莲节'],'6-25':['五谷母节'],'7-14':['中元节'],'7-22':['财神节'],'7-29':['地藏节'],'8-1':['天灸日'],'10-1':['寒衣节'],'10-10':['十成节'],'10-15':['下元节'],'12-7':['驱傩日'],'12-16':['尾牙'],'12-24':['祭灶日']},
OTHER_FESTIVAL:{'1-4':['接神日'],'1-5':['隔开日'],'1-7':['人日'],'1-8':['谷日','顺星节'],'1-9':['天日'],'1-10':['地日'],'1-20':['天穿节'],'1-25':['填仓节'],'1-30':['正月晦'],'2-1':['中和节'],'2-2':['社日节'],'3-3':['上巳节'],'5-20':['分龙节'],'5-25':['会龙节'],'6-6':['天贶节'],'6-24':['观莲节'],'6-25':['五谷母节'],'7-14':['中元节'],'7-22':['财神节'],'7-29':['地藏节'],'8-1':['天灸日'],'10-1':['寒衣节'],'10-10':['十成节'],'10-15':['下元节'],'12-7':['驱傩日'],'12-16':['尾牙'],'12-24':['祭灶日']},
CHONG:{'子':'午','丑':'未','寅':'申','卯':'酉','辰':'戌','巳':'亥','午':'子','未':'丑','申':'寅','酉':'卯','戌':'辰','亥':'巳'},
CHONG_GAN:{'甲':'戊','乙':'己','丙':'庚','丁':'辛','戊':'壬','己':'癸','庚':'甲','辛':'乙','壬':'丙','癸':'丁'},
CHONG_GAN_BAD:{'庚':'甲','辛':'乙','壬':'丙','癸':'丁'},
@@ -3520,7 +3558,8 @@
},
getShenGongNaYin:function(){return LunarUtil.NAYIN[this.getShenGong()];},
getLunar:function(){return this._p.lunar;},
getYun:function(gender){
getYun:function(gender, sect){
sect = (2 == sect) ? sect : 1;
var lunar = this.getLunar();
var yang = 0 === lunar.getYearGanIndexExact() % 2;
var man = 1 === gender;
@@ -3531,25 +3570,44 @@
var current = lunar.getSolar();
var start = forward ? current : prev.getSolar();
var end = forward ? next.getSolar() : current;
const endTimeZhiIndex = (end.getHour() === 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substr(11, 5));
const startTimeZhiIndex = (start.getHour() === 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substr(11, 5));
// 时辰差
var hourDiff = endTimeZhiIndex - startTimeZhiIndex;
// 天数差
var dayDiff = ExactDate.getDaysBetweenYmd(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;
var year;
var month;
var day;
var hour = 0;
if (2 === sect) {
var minutes = Math.floor((end.getCalendar() - start.getCalendar()) / 60000);
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 {
var endTimeZhiIndex = (end.getHour() === 23) ? 11 : LunarUtil.getTimeZhiIndex(end.toYmdHms().substr(11, 5));
var startTimeZhiIndex = (start.getHour() === 23) ? 11 : LunarUtil.getTimeZhiIndex(start.toYmdHms().substr(11, 5));
// 时辰差
var hourDiff = endTimeZhiIndex - startTimeZhiIndex;
// 天数差
var dayDiff = ExactDate.getDaysBetweenYmd(start.getYear(), start.getMonth(), start.getDay(), end.getYear(), end.getMonth(), end.getDay());
if (hourDiff < 0) {
hourDiff += 12;
dayDiff--;
}
var monthDiff = Math.floor(hourDiff * 10 / 30);
month = dayDiff * 4 + monthDiff;
day = hourDiff * 10 - monthDiff * 30;
year = Math.floor(month / 12);
month = month - year * 12;
}
var monthDiff = Math.floor(hourDiff * 10 / 30);
var month = dayDiff * 4 + monthDiff;
var day = hourDiff * 10 - monthDiff * 30;
var year = Math.floor(month / 12);
month = month - year * 12;
return {
year: year,
month: month,
day: day
day: day,
hour: hour
};
})();
var buildLiuYue = function(liuNian, index){
@@ -3729,6 +3787,7 @@
startYear: start.year,
startMonth: start.month,
startDay: start.day,
startHour: start.hour,
forward: forward,
lunar: lunar
},
@@ -3736,6 +3795,7 @@
getStartYear: function(){return this._p.startYear;},
getStartMonth: function(){return this._p.startMonth;},
getStartDay: function(){return this._p.startDay;},
getStartHour: function(){return this._p.startHour;},
isForward: function(){return this._p.forward;},
getLunar: function(){return this._p.lunar;},
getStartSolar: function(){
@@ -3744,6 +3804,7 @@
c.setFullYear(birth.getYear() + this._p.startYear);
c.setMonth(birth.getMonth()-1+this._p.startMonth);
c.setDate(birth.getDay() + this._p.startDay);
c.setHours(birth.getHour() + this._p.startHour);
return Solar.fromDate(c);
},
getDaYun: function(n){

View File

@@ -1,6 +1,6 @@
{
"name": "lunar-javascript",
"version": "1.2.27",
"version": "1.2.30",
"description": "lunar is a calendar library for Solar and Chinese Lunar.",
"main": "index.js",
"scripts": {