Lunar Calendar Converter

0.0.9 · active · verified Sat Apr 11

LunarCalendar is a Python library for converting between solar (Gregorian) and lunar calendars, primarily focused on the Chinese lunisolar calendar. It includes support for 24 solar terms and numerous Chinese lunar and solar holidays. The latest stable version on PyPI is 0.0.9, released in 2018. While the PyPI release is older, the project on GitHub appears functional, supporting a time range of 1900 to 2100. It's suitable for applications requiring accurate Chinese calendar calculations.

Warnings

Install

Imports

Quickstart

Demonstrates converting a solar date to a lunar date and vice-versa, and shows how to handle `DateNotExist` for invalid lunar date inputs.

import datetime
from lunarcalendar import Converter, Solar, Lunar, DateNotExist

# Solar to Lunar conversion
solar_date = Solar(2024, 4, 11) # Example: April 11, 2024
print(f"Solar date: {solar_date}")
lunar_date = Converter.Solar2Lunar(solar_date)
print(f"Converted Lunar date: {lunar_date}")

# Lunar to Solar conversion
lunar_date_example = Lunar(2024, 3, 3) # Example: 3rd day of 3rd lunar month, 2024
print(f"Lunar date: {lunar_date_example}")
solar_date_converted = Converter.Lunar2Solar(lunar_date_example)
print(f"Converted Solar date: {solar_date_converted}")

# Handling invalid lunar dates
try:
    invalid_lunar = Lunar(2024, 2, 30, isleap=False) # February 30th is invalid in any lunar calendar
except DateNotExist as e:
    print(f"Caught expected error for invalid date: {e}")

view raw JSON →