### Use lunar with npm Source: https://github.com/6tail/lunar-javascript/blob/master/README_EN.md Install the package via npm and require the Solar class to perform date conversions. ```javascript //test.js const {Solar} = require('lunar-javascript') //const {Solar, Lunar, HolidayUtil} = require('lunar-javascript') let solar = Solar.fromYmd(1986,5,29); console.log(solar.toFullString()); console.log(solar.getLunar().toFullString()); ``` -------------------------------- ### SolarYear Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt Work with solar calendar years. Use `SolarYear.fromYear()` to create a SolarYear object and `next()` to get subsequent years. ```javascript const { SolarYear } = require('lunar-javascript'); // Solar year const year = SolarYear.fromYear(2019); console.log(year.toString()); // "2019" console.log(year.toFullString()); // "2019年" console.log(year.next(1).toString()); // "2020" ``` -------------------------------- ### Auspicious and Inauspicious Day Calculations Source: https://context7.com/6tail/lunar-javascript/llms.txt Get detailed almanac information for traditional Chinese calendar purposes, such as Tai Sui position, Fu direction, and Lu information. Use `Solar.fromYmd().getLunar()` to get the lunar representation of a solar date. ```javascript const { Solar } = require('lunar-javascript'); const lunar = Solar.fromYmd(2021, 11, 13).getLunar(); // Get Tai Sui (胎神) position console.log(lunar.getDayPositionTai()); // "碓磨厕 外东南" // Get Fu (福神) direction console.log(lunar.getDayPositionFuDesc()); // "西南" ``` ```javascript // Get Lu (禄) information const lunar2 = Solar.fromYmd(2017, 2, 15).getLunar(); console.log(lunar2.getDayLu()); // "子命互禄 辛命进禄" ``` ```javascript // Get other festivals (社日 etc.) const lunar3 = Solar.fromYmd(2022, 3, 16).getLunar(); console.log(lunar3.getOtherFestivals()); // ["春社"] ``` ```javascript // Get auspicious gods (吉神宜趋) const lunar4 = Solar.fromYmd(2025, 9, 16).getLunar(); console.log(lunar4.getDayJiShen().toString()); // "时德,阳德,民日,玉宇,司命" ``` ```javascript // Get inauspicious spirits (凶煞宜忌) console.log(lunar4.getDayXiongSha().toString()); // "河魁,死神,天吏,致死,往亡" ``` -------------------------------- ### LunarMonth Information and Leap Months Source: https://context7.com/6tail/lunar-javascript/llms.txt Get information about a specific lunar month, including handling leap months using negative month numbers. Use `LunarMonth.fromYm()` to create a LunarMonth object. ```javascript const { LunarMonth } = require('lunar-javascript'); // Get lunar month const month = LunarMonth.fromYm(2023, 1); console.log(month.getIndex()); // 1 console.log(month.getGanZhi()); // "甲寅" ``` ```javascript // Handle leap months (use negative month number) const leapMonth = LunarMonth.fromYm(2023, -2); // Leap 2nd month console.log(leapMonth.getIndex()); // 3 console.log(leapMonth.getGanZhi()); // "乙卯" ``` ```javascript // Regular month after leap const monthAfterLeap = LunarMonth.fromYm(2023, 3); console.log(monthAfterLeap.getIndex()); // 4 console.log(monthAfterLeap.getGanZhi()); // "丙辰" ``` -------------------------------- ### LunarYear Information and Calculations Source: https://context7.com/6tail/lunar-javascript/llms.txt Get detailed information about a lunar year, including leap month details and traditional calculations. Use `LunarYear.fromYear()` to create a LunarYear object. ```javascript const { LunarYear } = require('lunar-javascript'); // Create from year const year = LunarYear.fromYear(2020); // Get leap month information const leapMonth = year.getMonth(-4); // Leap 4th month console.log(leapMonth.toString()); // "2020年闰四月(29)天" ``` ```javascript // Get traditional calculations const year2017 = LunarYear.fromYear(2017); console.log(year2017.getZhiShui()); // "二龙治水" (Water control) console.log(year2017.getFenBing()); // "二人分饼" (Cake sharing) console.log(year2017.getGengTian()); // Ox plowing field ``` ```javascript // Get Yuan and Yun (三元九运) const year1864 = LunarYear.fromYear(1864); console.log(year1864.getYuan()); // "上元" (Upper Yuan) console.log(year1864.getYun()); // "一运" (First Yun) const year1984 = LunarYear.fromYear(1984); console.log(year1984.getYuan()); // "下元" (Lower Yuan) const year2004 = LunarYear.fromYear(2004); console.log(year2004.getYun()); // "八运" (Eighth Yun) ``` ```javascript // Get day count for the year const year2021 = LunarYear.fromYear(2021); console.log(year2021.getDayCount()); // 354 (normal year) const year2023 = LunarYear.fromYear(2023); console.log(year2023.getDayCount()); // 384 (leap year) ``` -------------------------------- ### Initialize lunar in a Browser Source: https://github.com/6tail/lunar-javascript/blob/master/README_EN.md Include the library via a script tag to access the Solar and Lunar classes in the global scope. ```html ``` -------------------------------- ### SolarWeek and SolarMonth Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt Work with solar calendar weeks and months. Use `SolarWeek.fromYmd()` or `SolarMonth.fromYm()` to create objects. `SolarUtil` provides utility functions. ```javascript const { SolarWeek, SolarMonth, SolarUtil } = require('lunar-javascript'); // Create week starting from Monday (1) or Sunday (0) const weekFromMonday = SolarWeek.fromYmd(2019, 5, 1, 1); console.log(weekFromMonday.toString()); // "2019.5.1" console.log(weekFromMonday.toFullString()); // "2019年5月第1周" console.log(weekFromMonday.getFirstDay().toString()); // "2019-04-29" console.log(weekFromMonday.getFirstDayInMonth().toString()); // "2019-05-01" ``` ```javascript // Get number of weeks in a month console.log(SolarUtil.getWeeksOfMonth(2019, 5, 1)); // 5 ``` ```javascript // Week index const week = SolarWeek.fromYmd(2022, 5, 1, 0); console.log(week.getIndex()); // 1 console.log(week.getIndexInYear()); // Week number in year ``` ```javascript // Work with months const month = SolarMonth.fromYm(2019, 5); console.log(month.toString()); // "2019-5" console.log(month.toFullString()); // "2019年5月" console.log(month.next(1).toString()); // "2019-6" ``` ```javascript // Get weeks in a month const month2 = SolarMonth.fromYm(2022, 7); const weeks = month2.getWeeks(0); // Starting from Sunday const lastWeek = weeks[weeks.length - 1]; const days = lastWeek.getDays(); console.log(days[0].toFullString()); // First day of last week ``` -------------------------------- ### Calculate Solar Terms (JieQi) in JavaScript Source: https://context7.com/6tail/lunar-javascript/llms.txt Use the Solar and Lunar classes to retrieve current, previous, and next solar terms, or generate a table for a specific lunar month. ```javascript const { Solar, Lunar } = require('lunar-javascript'); // Get current solar term const solar = Solar.fromYmd(1986, 1, 5); const lunar = solar.getLunar(); console.log(lunar.getJie()); // "小寒" (Minor Cold) console.log(lunar.getQi()); // "" (empty if not a Qi day) console.log(lunar.getJieQi()); // "小寒" // Check current solar term object console.log(lunar.getCurrentJie() + ''); // "小寒" console.log(lunar.getCurrentQi()); // null (not a Qi) // Navigate between solar terms console.log(lunar.getPrevJie().getName()); // "大雪" (Major Snow) console.log(lunar.getPrevQi().getName()); // "冬至" (Winter Solstice) console.log(lunar.getNextJie().getName()); // "大寒" or next Jie console.log(lunar.getNextQi().getName()); // "大寒" or next Qi // Get solar term table for a lunar month const lunarMonth = Lunar.fromYmd(2012, 9, 1); const jieQiTable = lunarMonth.getJieQiTable(); console.log(jieQiTable['白露'].toYmdHms()); // "2012-09-07 13:29:01" // Distinguish Jie and Qi on their respective days const winterSolstice = Solar.fromYmd(2021, 12, 21).getLunar(); console.log(winterSolstice.getJieQi()); // "冬至" console.log(winterSolstice.getJie()); // "" (冬至 is a Qi, not a Jie) console.log(winterSolstice.getQi()); // "冬至" ``` -------------------------------- ### Manage Chinese Holidays with HolidayUtil Source: https://context7.com/6tail/lunar-javascript/llms.txt Manage Chinese statutory holidays and custom holidays. Retrieve holiday information by date or customize holidays using the fix method. ```javascript const { HolidayUtil } = require('lunar-javascript'); // Get holiday information const holiday = HolidayUtil.getHoliday('2020-01-01'); console.log(holiday + ''); // "2020-01-01 元旦节 2020-01-01" console.log(holiday.getName()); // "元旦节" console.log(holiday.getTarget()); // Target date for multi-day holidays // Get holiday by year, month, day const holiday2 = HolidayUtil.getHoliday(2025, 1, 1); console.log(holiday2.getName()); // Customize holidays using fix method // Format: YYYYMMDDTNYYYYMMDD where T is type (0=holiday, 1=work day) // N is holiday name index HolidayUtil.fix('202001011120200101'); // Modify 2020-01-01 to Spring Festival console.log(HolidayUtil.getHoliday('2020-01-01').getName()); // "春节" // Add new holiday HolidayUtil.fix('209901010120990101'); // Add 2099-01-01 as New Year console.log(HolidayUtil.getHoliday('2099-01-01').getName()); // Customize holiday names let names = HolidayUtil.NAMES; names[0] = '元旦'; // Index 0 names[1] = '大年初一'; // Index 1 HolidayUtil.fix(names, ''); // Add custom holidays with custom names names[9] = '我的生日'; names[10] = '结婚纪念日'; HolidayUtil.fix(names, '20210529912021052920211111:12021111120211201;120211201'); console.log(HolidayUtil.getHoliday('2021-05-29').getName()); // "我的生日" // Remove a holiday HolidayUtil.fix('20100101~000000000000000000000000000'); console.log(HolidayUtil.getHoliday('2010-01-01')); // null ``` -------------------------------- ### Use lunar in Node.js Source: https://github.com/6tail/lunar-javascript/blob/master/README_EN.md Require the local lunar.js file directly to use the library in a Node.js environment. ```javascript //test.js const {Solar} = require('./lunar.js') //const {Solar, Lunar, HolidayUtil} = require('./lunar.js') let solar = Solar.fromYmd(1986,5,29); console.log(solar.toFullString()); console.log(solar.getLunar().toFullString()); ``` -------------------------------- ### Buddhist Calendar Calculations with Foto Source: https://context7.com/6tail/lunar-javascript/llms.txt Perform Buddhist calendar calculations, including Buddhist year, festivals, and star information. Create a Foto object from a Lunar date. ```javascript const { Foto, Lunar } = require('lunar-javascript'); // Create Foto from Lunar date const foto = Foto.fromLunar(Lunar.fromYmd(2021, 10, 14)); console.log(foto.toFullString()); // Output: "二五六五年十月十四 (三元降) (四天王巡行)" // Get star information (28 Mansions) const foto2 = Foto.fromLunar(Lunar.fromYmd(2020, 4, 13)); console.log(foto2.getXiu()); // "氐" (Mansion name) console.log(foto2.getZheng()); // "土" (Element) console.log(foto2.getAnimal()); // "貉" (Animal) console.log(foto2.getGong()); // "东" (Direction) console.log(foto2.getShou()); // "青龙" (Mythical creature) // Get Buddhist festivals const foto3 = Foto.fromLunar(Lunar.fromYmd(2021, 3, 16)); console.log(foto3.getOtherFestivals()); // ["准提菩萨圣诞"] ``` -------------------------------- ### Perform Solar Calendar Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt Use the Solar class to create, navigate, and retrieve information from Gregorian dates. SolarUtil provides static utility methods like leap year checks. ```javascript const { Solar, SolarUtil } = require('lunar-javascript'); // Create Solar date from year, month, day const solar = Solar.fromYmd(2019, 5, 1); console.log(solar.toString()); // "2019-05-01" console.log(solar.toFullString()); // "2019-05-01 00:00:00 星期三 (劳动节) 金牛座" // Create Solar date with time const solarWithTime = Solar.fromYmdHms(2020, 5, 24, 13, 0, 0); console.log(solarWithTime.getLunar().toString()); // "二〇二〇年闰四月初二" // Create from JavaScript Date object const today = Solar.fromDate(new Date()); console.log(today.toYmd()); // e.g., "2024-01-15" // Get zodiac sign (constellation) const zodiac = Solar.fromYmd(2020, 5, 21); console.log(zodiac.getXingZuo()); // "双子" (Gemini) // Navigate dates const nextMonth = solar.nextMonth(2); console.log(nextMonth.toYmd()); // "2019-07-01" const nextYear = solar.nextYear(2); console.log(nextYear.toYmd()); // "2021-05-01" const nextDay = solar.nextDay(10); console.log(nextDay.toYmd()); // "2019-05-11" // Get day of week (0=Sunday, 1=Monday, ...) console.log(solar.getWeek()); // 3 (Wednesday) // Get festivals console.log(solar.getFestivals()); // ["劳动节"] // Check leap year console.log(SolarUtil.isLeapYear(2020)); // true ``` -------------------------------- ### Internationalization (I18n) Settings Source: https://context7.com/6tail/lunar-javascript/llms.txt Switch between Chinese and English output for lunar calendar information. Use `I18n.setLanguage()` to change the output language. ```javascript const { Lunar, I18n } = require('lunar-javascript'); const lunar = Lunar.fromYmd(2023, 1, 1); // Default Chinese output console.log(lunar.toFullString()); // Switch to English I18n.setLanguage('en'); console.log(lunar.toFullString()); // Switch back to Chinese I18n.setLanguage('chs'); console.log(lunar.toFullString()); ``` -------------------------------- ### Convert BaZi to Solar Dates Source: https://context7.com/6tail/lunar-javascript/llms.txt Reverse lookup of possible solar dates from a given set of BaZi pillars. ```javascript const { Solar } = require('lunar-javascript'); // Convert BaZi to possible Solar dates const solarList = Solar.fromBaZi('丙辰', '丁酉', '丙子', '甲午'); solarList.forEach(solar => { console.log(solar.toYmdHms()); }); // Output: "1916-10-06 12:00:00", "1976-09-21 12:00:00" // With sect parameter and start year filter const filtered = Solar.fromBaZi('癸卯', '甲寅', '甲寅', '甲子', 2, 1843); filtered.forEach(solar => { console.log(solar.toYmdHms()); }); // Output: "1843-02-09 00:00:00", "2023-02-25 00:00:00" // Another example const results = Solar.fromBaZi('己亥', '丁丑', '壬寅', '戊申'); results.forEach(solar => { console.log(solar.toYmdHms()); }); // Output: "1900-01-29 16:00:00", "1960-01-15 16:00:00" ``` -------------------------------- ### Calculate EightChar (BaZi) Destiny Pillars Source: https://context7.com/6tail/lunar-javascript/llms.txt Extract Four Pillars, hidden stems, Ten Gods, Twelve Stages, and NaYin from a birth date. ```javascript const { Solar, Lunar } = require('lunar-javascript'); // Get Eight Characters from a birth time const solar = Solar.fromYmdHms(2005, 12, 23, 8, 37, 0); const lunar = solar.getLunar(); const eightChar = lunar.getEightChar(); // Get the four pillars console.log(eightChar.getYear()); // "乙酉" (Year pillar) console.log(eightChar.getMonth()); // "戊子" (Month pillar) console.log(eightChar.getDay()); // "辛巳" (Day pillar) console.log(eightChar.getTime()); // "壬辰" (Hour pillar) console.log(eightChar.toString()); // "乙酉 戊子 辛巳 壬辰" // Get hidden stems (藏干) console.log(eightChar.getYearHideGan() + ''); // "辛" console.log(eightChar.getMonthHideGan() + ''); // "癸" console.log(eightChar.getDayHideGan() + ''); // "丙,庚,戊" console.log(eightChar.getTimeHideGan() + ''); // "戊,乙,癸" // Get Ten Gods (十神) console.log(eightChar.getYearShiShenGan()); // "偏财" console.log(eightChar.getMonthShiShenGan()); // "正印" console.log(eightChar.getDayShiShenGan()); // "日主" console.log(eightChar.getTimeShiShenGan()); // "伤官" // Get Twelve Stages (地势/长生十二宫) console.log(eightChar.getYearDiShi()); // "临官" console.log(eightChar.getMonthDiShi()); // "长生" console.log(eightChar.getDayDiShi()); // "死" console.log(eightChar.getTimeDiShi()); // "墓" // Get NaYin (纳音五行) console.log(eightChar.getYearNaYin()); // "泉中水" console.log(eightChar.getMonthNaYin()); // "霹雳火" console.log(eightChar.getDayNaYin()); // "白蜡金" console.log(eightChar.getTimeNaYin()); // "长流水" // Get TaiYuan (胎元) and MingGong (命宫) console.log(eightChar.getTaiYuan()); // "己卯" console.log(eightChar.getMingGong()); // "己丑" console.log(eightChar.getShenGong()); // Body Palace (身宫) ``` -------------------------------- ### Calculate Major Fate Cycles (Da Yun) Source: https://context7.com/6tail/lunar-javascript/llms.txt Calculate Da Yun (major fate cycles) and Liu Nian (annual cycles) for fortune telling. Specify gender (0 for female, 1 for male) and optionally a calculation method. ```javascript const { Solar, Lunar } = require('lunar-javascript'); // Calculate fate cycles const solar = Solar.fromYmdHms(1981, 1, 29, 23, 37, 0); const lunar = solar.getLunar(); const eightChar = lunar.getEightChar(); // Get Yun (0 for female, 1 for male) const yun = eightChar.getYun(0); // Female // Get start information console.log(yun.getStartYear()); // 8 (years until first major cycle) console.log(yun.getStartMonth()); // 0 console.log(yun.getStartDay()); // 20 console.log(yun.getStartSolar().toYmd()); // "1989-02-18" // For male const yunMale = eightChar.getYun(1); console.log(yunMale.getStartSolar().toYmd()); // Access Da Yun cycles const daYunList = yun.getDaYun(); const firstDaYun = daYunList[0]; const liuNianList = firstDaYun.getLiuNian(); const liuYueList = liuNianList[0].getLiuYue(); console.log(liuYueList[0].getGanZhi()); // First month's GanZhi // With calculation method parameter (1 or 2) const yun2 = eightChar.getYun(1, 2); console.log(yun2.getStartYear()); console.log(yun2.getStartMonth()); console.log(yun2.getStartDay()); ``` -------------------------------- ### Solar Class Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt The Solar class handles Gregorian calendar dates, providing methods for creation, navigation, and conversion to Lunar dates. ```APIDOC ## Solar Class Operations ### Description The Solar class represents a Gregorian date and provides methods to create, manipulate, and convert dates. ### Methods - **Solar.fromYmd(year, month, day)**: Creates a Solar instance from year, month, and day. - **Solar.fromYmdHms(year, month, day, hour, minute, second)**: Creates a Solar instance with time. - **Solar.fromDate(date)**: Creates a Solar instance from a JavaScript Date object. - **solar.nextMonth(n)**: Returns a new Solar instance offset by n months. - **solar.nextYear(n)**: Returns a new Solar instance offset by n years. - **solar.nextDay(n)**: Returns a new Solar instance offset by n days. - **solar.getWeek()**: Returns the day of the week (0=Sunday, 1=Monday, etc.). - **solar.getFestivals()**: Returns an array of festivals for the date. - **SolarUtil.isLeapYear(year)**: Checks if a year is a leap year. ``` -------------------------------- ### Perform Lunar Calendar Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt Use the Lunar class for Chinese calendar calculations, including leap months and GanZhi. LunarYear provides access to specific year-based lunar data. ```javascript const { Solar, Lunar, LunarYear } = require('lunar-javascript'); // Create Lunar date from year, month, day const lunar = Lunar.fromYmd(2020, 4, 21); console.log(lunar.toString()); // "二〇二〇年四月廿一" console.log(lunar.getSolar().toString()); // "2020-05-13" // Create Lunar date with time const lunarWithTime = Lunar.fromYmdHms(2019, 12, 12, 11, 22, 0); console.log(lunarWithTime.getSolar().toString()); // "2020-01-06" // Convert from Solar to Lunar const solar = Solar.fromYmd(2019, 5, 1); const lunarFromSolar = solar.getLunar(); console.log(lunarFromSolar.toString()); // "二〇一九年三月廿七" console.log(lunarFromSolar.toFullString()); // Output: 二〇一九年三月廿七 己亥(猪)年 戊辰(龙)月 戊戌(狗)日 子(鼠)时 ... // Handle leap months (use negative month number) const leapMonth = Lunar.fromYmd(2020, -4, 2); // Leap 4th month console.log(leapMonth.getMonthInChinese()); // "闰四" // Get zodiac animal (ShengXiao) console.log(lunarFromSolar.getYearShengXiao()); // "猪" // Get GanZhi (Heavenly Stems and Earthly Branches) const ganzhi = Solar.fromYmdHms(2020, 1, 1, 13, 22, 0).getLunar(); console.log(ganzhi.getYearInGanZhi()); // "己亥" console.log(ganzhi.getMonthInGanZhi()); // "丙子" console.log(ganzhi.getDayInGanZhi()); // Day's GanZhi // Get lunar festivals const newYear = Lunar.fromYmd(2021, 12, 29); console.log(newYear.getFestivals()); // ["除夕"] // Access lunar year information const year = LunarYear.fromYear(2020); const leapMonthInfo = year.getMonth(-4); // Get leap 4th month console.log(leapMonthInfo.toString()); // "2020年闰四月(29)天" ``` -------------------------------- ### Calculate Nine Star Astrology Source: https://context7.com/6tail/lunar-javascript/llms.txt Calculate the Nine Star (九星) for a given date, used in Feng Shui and Chinese astrology. Retrieve the number or full string representation. ```javascript const { Solar, Lunar } = require('lunar-javascript'); // Get year's Nine Star const lunar = Solar.fromYmd(1985, 2, 19).getLunar(); console.log(lunar.getYearNineStar().getNumber()); // "六" // Get full Nine Star information const lunar2 = Lunar.fromYmd(2022, 1, 1); console.log(lunar2.getYearNineStar().toString()); // "六白金开阳" const lunar3 = Lunar.fromYmd(2033, 1, 1); console.log(lunar3.getYearNineStar().toString()); // "四绿木天权" ``` -------------------------------- ### Lunar Class Operations Source: https://context7.com/6tail/lunar-javascript/llms.txt The Lunar class handles Chinese lunar dates, including leap month support, GanZhi calculations, and traditional festival lookups. ```APIDOC ## Lunar Class Operations ### Description The Lunar class represents a Chinese lunar date with support for leap months, traditional festivals, GanZhi, and zodiac animals. ### Methods - **Lunar.fromYmd(year, month, day)**: Creates a Lunar instance. Use negative month numbers for leap months. - **Lunar.fromYmdHms(year, month, day, hour, minute, second)**: Creates a Lunar instance with time. - **solar.getLunar()**: Converts a Solar instance to a Lunar instance. - **lunar.getYearShengXiao()**: Returns the zodiac animal (ShengXiao) for the year. - **lunar.getYearInGanZhi()**: Returns the year in Heavenly Stems and Earthly Branches. - **lunar.getMonthInGanZhi()**: Returns the month in Heavenly Stems and Earthly Branches. - **lunar.getDayInGanZhi()**: Returns the day in Heavenly Stems and Earthly Branches. - **lunar.getFestivals()**: Returns an array of lunar festivals. - **LunarYear.fromYear(year)**: Accesses information for a specific lunar year. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.