/** * 农历(阴历)万年历 * LunarCalendar * vervison : v0.1.4 * Github : https://github.com/zzyss86/LunarCalendar * HomePage : http://www.tuijs.com/ * Author : JasonZhou * Email : zzyss86@qq.com */ (function(){ var extend = function(o, c){ if(o && c && typeof c == "object"){ for(var p in c){ o[p] = c[p]; } } return o; }; var creatLenArr = function(year,month,len,start){ var arr = []; start = start || 0; if(len<1)return arr; var k = start; for(var i=0;i0 ? between : lunarYearDays.yearDays - Math.abs(between); var monthDays = lunarYearDays.monthDays; var tempDays = 0; var month = 0; for(var i=0;i end){ month = i; tempDays = tempDays-monthDays[i]; break; } } return [year,month,end - tempDays + 1]; }; /** * 根据距离正月初一的天数计算农历日期 * @param {Number} year 公历年,月,日 */ function getLunarByBetween(year,month,day){ var yearData = lunarInfo[year-minYear]; var zenMonth = yearData[1]; var zenDay = yearData[2]; var between = getDaysBetweenSolar(year,zenMonth-1,zenDay,year,month,day); if(between==0){ //正月初一 return [year,0,1]; }else{ var lunarYear = between>0 ? year : year-1; return getLunarDateByBetween(lunarYear,between); } }; /** * 两个公历日期之间的天数 */ function getDaysBetweenSolar(year,month,day,year1,month1,day1){ var date = new Date(year,month,day).getTime(); var date1 = new Date(year1,month1,day1).getTime(); return (date1-date) / 86400000; }; /** * 计算农历日期离正月初一有多少天 * @param {Number} year,month,day 农年,月(0-12,有闰月),日 */ function getDaysBetweenZheng(year,month,day){ var lunarYearDays = getLunarYearDays(year); var monthDays = lunarYearDays.monthDays; var days = 0; for(var i=0;i maxYear)return {error:100, msg:errorCode[100]}; return { year : year, month : month, day : day }; }; /** * 将农历转换为公历 * @param {Number} year,month,day 农历年,月(1-13,有闰月),日 */ function lunarToSolar(_year,_month,_day){ var inputDate = formateDate(_year,_month,_day); if(inputDate.error)return inputDate; var year = inputDate.year; var month = inputDate.month; var day = inputDate.day; var between = getDaysBetweenZheng(year,month,day); //离正月初一的天数 var yearData = lunarInfo[year-minYear]; var zenMonth = yearData[1]; var zenDay = yearData[2]; var offDate = new Date(year,zenMonth-1,zenDay).getTime() + between * 86400000; offDate = new Date(offDate); return { year : offDate.getFullYear(), month : offDate.getMonth()+1, day : offDate.getDate() }; }; /** * 将公历转换为农历 * @param {Number} year,month,day 公历年,月,日 */ function solarToLunar(_year,_month,_day){ var inputDate = formateDate(_year,_month,_day,minYear); if(inputDate.error)return inputDate; var year = inputDate.year; var month = inputDate.month; var day = inputDate.day; cacheUtil.setCurrent(year); //立春日期 var term2 = cacheUtil.get('term2') ? cacheUtil.get('term2') : cacheUtil.set('term2',getTerm(year,2)); //二十四节气 var termList = cacheUtil.get('termList') ? cacheUtil.get('termList') : cacheUtil.set('termList',getYearTerm(year)); var firstTerm = getTerm(year,month*2); //某月第一个节气开始日期 var GanZhiYear = (month>1 || month==1 && day>=term2) ? year+1 : year;//干支所在年份 var GanZhiMonth = day>=firstTerm ? month+1 : month; //干支所在月份(以节气为界) var lunarDate = getLunarByBetween(year,month,day); var lunarLeapMonth = getLunarLeapYear(lunarDate[0]); var lunarMonthName = ''; if(lunarLeapMonth>0 && lunarLeapMonth==lunarDate[1]){ lunarMonthName = '闰'+DATA.monthCn[lunarDate[1]-1]+'月'; }else if(lunarLeapMonth>0 && lunarDate[1]>lunarLeapMonth){ lunarMonthName = DATA.monthCn[lunarDate[1]-1]+'月'; }else{ lunarMonthName = DATA.monthCn[lunarDate[1]]+'月'; } //农历节日判断 var lunarFtv = ''; var lunarMonthDays = getLunarYearDays(lunarDate[0]).monthDays; //除夕 if(lunarDate[1] == lunarMonthDays.length-1 && lunarDate[2]==lunarMonthDays[lunarMonthDays.length-1]){ lunarFtv = lunarFestival['d0100']; }else if(lunarLeapMonth>0 && lunarDate[1]>lunarLeapMonth){ lunarFtv = lunarFestival[formateDayD4(lunarDate[1]-1,lunarDate[2])]; }else{ lunarFtv = lunarFestival[formateDayD4(lunarDate[1],lunarDate[2])]; } var res = { zodiac : getYearZodiac(GanZhiYear), GanZhiYear : getLunarYearName(GanZhiYear), GanZhiMonth : getLunarMonthName(year,GanZhiMonth), GanZhiDay : getLunarDayName(year,month,day), //放假安排:0无特殊安排,1工作,2放假 worktime : worktime['y'+year] && worktime['y'+year][formateDayD4(month,day)] ? worktime['y'+year][formateDayD4(month,day)] : 0, term : termList[formateDayD4(month,day)], lunarYear : lunarDate[0], lunarMonth : lunarDate[1]+1, lunarDay : lunarDate[2], lunarMonthName : lunarMonthName, lunarDayName : DATA.dateCn[lunarDate[2]-1], lunarLeapMonth : lunarLeapMonth, solarFestival : solarFestival[formateDayD4(month,day)], lunarFestival : lunarFtv }; return res; }; /** * 获取指定公历月份的农历数据 * return res{Object} * @param {Number} year,month 公历年,月 * @param {Boolean} fill 是否用上下月数据补齐首尾空缺,首例数据从周日开始 */ function calendar(_year,_month,fill){ var inputDate = formateDate(_year,_month); if(inputDate.error)return inputDate; var year = inputDate.year; var month = inputDate.month; var calendarData = solarCalendar(year,month+1,fill); for(var i=0;i 0){ //前补 var preYear = month-1<0 ? year-1 : year; var preMonth = month-1<0 ? 11 : month-1; preMonthDays = getSolarMonthDays(preYear,preMonth); preMonthData = creatLenArr(preYear,preMonth+1,res.firstDay,preMonthDays-res.firstDay+1); res.monthData = preMonthData.concat(res.monthData); } if(7*6 - res.monthData.length!=0){ //后补 var nextYear = month+1>11 ? year+1 : year; var nextMonth = month+1>11 ? 0 : month+1; var fillLen = 7*6 - res.monthData.length; nextMonthData = creatLenArr(nextYear,nextMonth+1,fillLen,1); res.monthData = res.monthData.concat(nextMonthData); } } return res; }; /** * 设置放假安排【对外暴露接口】 * @param {Object} workData */ function setWorktime(workData){ extend(worktime,workData); }; var LunarCalendar = { solarToLunar : solarToLunar, lunarToSolar : lunarToSolar, calendar : calendar, solarCalendar : solarCalendar, setWorktime : setWorktime, getSolarMonthDays : getSolarMonthDays }; if (typeof define === 'function'){ define (function (){ return LunarCalendar; }); }else if(typeof exports === 'object'){ module.exports = LunarCalendar; }else{ window.LunarCalendar = LunarCalendar; }; })();