lunar-javascript

repository·master·Indexed 23 days ago

https://github.com/6tail/lunar-javascript

A zero-dependency JavaScript library for Gregorian (Solar), Lunar, Buddhist, and Taoist calendars. It provides comprehensive support for traditional Chinese astrological and almanac data, including the Sexagenary cycle, zodiac animals, solar terms, Bazi (Eight Characters), and various auspicious/inauspicious calculations. The library includes core classes such as Solar, Lunar, Tao, and EightChar, along with utility classes for holidays and internationalization.

Tokens
1.9K
Snippets
8
Records
12
Agent score
82%

What's inside lunar-javascript

  1. Overview of lunar-javascript capabilities

    master

    lunar-javascript is a zero-dependency library for handling various calendar systems and traditional Chinese astrological data.

    Supported features include:

    • Calendars: Gregorian (Solar), Lunar (including Old Almanac/Huangli), Buddhist, and Taoist calendars.
    • Astrology & Zodiac: Constellations, Sexagenary cycle (Gan-Zhi), Zodiac animals, Solar terms, and Festivals.
    • Traditional Almanac Data: Pengzu Hundred Taboos, Daily auspicious/inauspicious activities, Auspicious gods and directions, Flying God (Tai Shen) directions, Clashes and Sha, Na Yin, Twenty-eight Mansions, Bazi (Eight Characters), Five Elements, Ten Gods, Twelve Jianchu Stars, and Qinglong Mingtang (Twelve Gods).
    • Other: Julian Day and auspicious/inauspicious days.
  2. Convert Solar dates to Lunar dates

    master

    The library allows you to create a Solar object using Solar.fromYmd(year, month, day). From a Solar instance, you can call .getLunar() to retrieve the corresponding Lunar object. Both objects support .toFullString() to output a detailed string containing solar information, lunar dates, zodiac signs, stems/branches, and other traditional Chinese calendar details.

    let solar = Solar.fromYmd(1986,5,29);
    console.log(solar.toFullString());
    console.log(solar.getLunar().toFullString());
  3. Use lunar-javascript in a browser

    master

    You can use the library in a standard HTML page by including the lunar.js script. This allows you to work with Solar and Lunar calendars directly in the browser.

    <!DOCTYPE html>
    <html
      >
      <head>
        <meta charset="utf-8">
      </head>
      <body
        >
        <script src="lunar.js"></script>
        <script
          >
          var solar = Solar.fromYmd(1986,5,29);
          console.log(solar.toFullString());
          console.log(solar.getLunar().toFullString());
        </script
      </body
    </html
  4. Use lunar-javascript in a web browser

    master

    To use the library in a standard HTML page, include the lunar.js script file. You can then use the Solar class to create solar dates and convert them to lunar dates.

    <!DOCTYPE html>
    <html
      >
      <head>
        <meta charset="utf-8">
      </head>
      <body
        >
        <script src="lunar.js"></script>
        <script
          >
          var solar = Solar.fromYmd(1986,5,29);
          console.log(solar.toFullString());
          console.log(solar.getLunar().toFullString());
        </script
      </body
    ></html>
  5. Use lunar-javascript in Node.js via npm

    master

    After installing via npm, import the Solar class from lunar-javascript. You can also import Lunar and HolidayUtil if needed.

    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());
  6. Use lunar-javascript in Node.js

    master

    In a Node.js environment, import Solar (and optionally Lunar or HolidayUtil) from the lunar-javascript package. You can then create Solar instances to convert between Gregorian and Lunar calendars.

    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());
  7. Use lunar-javascript in Node.js via local file

    master

    If you are not using npm, you can require the local lunar.js file directly.

    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());
  8. Import the lunar-javascript library

    master

    You can access the main classes and utilities of lunar-javascript by requiring the package. The library exports several core modules for handling Solar and Lunar calendars, traditional Chinese metaphysics (Tao, NineStar, EightChar), and various utility classes for holidays and time calculations.

    const { 
      Solar, 
      Lunar, 
      Foto, 
      Tao, 
      NineStar, 
      EightChar, 
      SolarWeek, 
      SolarMonth, 
      SolarSeason, 
      SolarHalfYear, 
      SolarYear, 
      LunarMonth, 
      LunarYear, 
      LunarTime, 
      ShouXingUtil, 
      SolarUtil, 
      LunarUtil, 
      FotoUtil, 
      TaoUtil, 
      HolidayUtil, 
      NineStarUtil, 
      I18n 
    } = require('lunar-javascript');
  9. Solar and Lunar Time Units

    master

    The library exports specific classes to represent various subdivisions of the solar and lunar years:

    Solar Units:

    • SolarWeek: Solar week.
    • SolarMonth: Solar month.
    • SolarSeason: Solar season.
    • SolarHalfYear: Solar half-year.
    • SolarYear: Solar year.

    Lunar Units:

    • LunarMonth: Lunar month.
    • LunarYear: Lunar year.
    • LunarTime: Lunar time components.
  10. Core Calendar and Metaphysics Classes

    master

    The library provides primary classes for calendar conversions and traditional Chinese calculations:

    • Solar: Represents a solar date.
    • Lunar: Represents a lunar date.
    • Foto: Likely related to photographic or visual representations of dates.
    • Tao: Related to Taoist calculations.
    • NineStar: Related to the Nine Star (Jiuxing) system.
    • EightChar: Related to the Eight Characters (Bazi) system.
  11. Utility Classes

    master

    A suite of utility classes is available for specialized calculations and internationalization:

    • SolarUtil: Utilities for solar calendar operations.
    • LunarUtil: Utilities for lunar calendar operations.
    • HolidayUtil: Utilities for calculating holidays.
    • NineStarUtil: Utilities for Nine Star calculations.
    • TaoUtil: Utilities for Tao calculations.
    • FotoUtil: Utilities for Foto operations.
    • ShouXingUtil: Utilities for ShouXing (Life Span/Star) calculations.
    • I18n: Internationalization support.