Calendar

Months of a lunar year, Gregorian ↔ lunar conversion, and the stem-branch of a lunar year — on the same calendar core as charting.

Calendar lookups without casting a chart: which months a lunar year has, how long each is and which one is leap, Gregorian ↔ lunar conversion, and the stem-branch of a lunar year. They run on the same calendar core as charting (a function-by-function port of lunar-typescript), so they agree day for day with the chart's rawDates.lunarDate and year pillar; golden data generated by lunar-typescript checks every day of lunar years 1–9999.

Years 1–9999 are supported, Gregorian and lunar alike. Dates before 1582-10-15 are Julian, and 1582-10-05 to 10-14 do not exist.

Errors

The conversions that take a full date (calendar.solarToLunar, calendar.lunarToSolar) return IztroError (code invalid_date) for a date that does not exist, like the charting entry points; the lookups that take only a year (calendar.lunarMonths, calendar.lunarYearPillar) return IztroError (code invalid_argument) for a year out of range, like horoscopes by lunar year.

import { calendar } from 'x-iztro';

calendar.lunarMonths

Purpose Every month of a lunar year: its number, whether it is leap, its length, and the Gregorian date of its first day.

Signature

calendar.lunarMonths(year: number): LunarMonthInfo[]

Parameters

ParameterTypeRequiredDefaultDescription
yearnumberYes—Lunar year, 1–9999

Returns LunarMonthInfo[] in calendar order; a leap month directly follows the month it repeats. Each item:

FieldTypeDescription
monthnumberMonth 1–12; a leap month carries the number it repeats, so leap 4th month is 4
isLeapbooleanWhether the month is leap
dayCountnumberLength: 29 (short month) or 30 (long month)
firstDayRawSolarDateGregorian date of the first day: solarYear, solarMonth, solarDay

Example 2023 has a leap second month:

for (const m of calendar.lunarMonths(2023).slice(0, 4)) {
  const d = m.firstDay;
  console.log(m.month, m.isLeap, m.dayCount, `${d.solarYear}-${d.solarMonth}-${d.solarDay}`);
}

Output

1 false 29 2023-1-22
2 false 30 2023-2-20
2 true 29 2023-3-22
3 false 29 2023-4-20

Edge cases


calendar.solarToLunar

Purpose Convert a Gregorian date to the lunar calendar.

Signature

calendar.solarToLunar(year: number, month: number, day: number): RawLunarDate

Parameters

ParameterTypeRequiredDefaultDescription
yearnumberYes—Gregorian year, 1–9999
monthnumberYes—Gregorian month, 1–12
daynumberYes—Gregorian day

Returns RawLunarDate: lunarYear, lunarMonth (1–12), lunarDay, isLeap — the same type as the chart's rawDates.lunarDate.

Example 2023-3-22 is the first day of the leap second month:

const l = calendar.solarToLunar(2023, 3, 22);
console.log(l.lunarYear, l.lunarMonth, l.lunarDay, l.isLeap);

Output

2023 2 1 true

Edge cases


calendar.lunarToSolar

Purpose Convert a lunar date to the Gregorian calendar.

Signature

calendar.lunarToSolar(
  year: number,
  month: number,
  day: number,
  options?: { isLeap?: boolean },
): RawSolarDate

Parameters

ParameterTypeRequiredDefaultDescription
yearnumberYes—Lunar year, 1–9999
monthnumberYes—Lunar month, 1–12; for a leap month, the number it repeats
daynumberYes—Lunar day, within the month's length
options.isLeapbooleanNofalseThe day is in the leap month

Returns RawSolarDate: solarYear, solarMonth, solarDay.

Example

const s = calendar.lunarToSolar(2023, 2, 1, { isLeap: true });
console.log(`${s.solarYear}-${s.solarMonth}-${s.solarDay}`);

// 2024 has no leap second month: an error, not the regular second month
try {
  calendar.lunarToSolar(2024, 2, 1, { isLeap: true });
} catch (e) {
  console.log(e.code);
}

Output

2023-3-22
invalid_date

Edge cases


calendar.lunarYearPillar

Purpose The stem-branch of a lunar year, as language-independent stem and branch keys.

Signature

calendar.lunarYearPillar(year: number): Pillar

Parameters

ParameterTypeRequiredDefaultDescription
yearnumberYes—Year number, 1–9999

Returns Pillar ([stem key, branch key]). Year 4 is jia-zi, and the cycle runs on by year number from there.

Example

const [stem, branch] = calendar.lunarYearPillar(2024);
console.log(stem, branch);

Output

jiaHeavenly chenEarthly

Year boundary The stem-branch depends only on the year number, not on year_divide; the boundary setting decides from which day that pillar applies. The chart's year pillar (rawDates.chineseDate.pillarKeys()[0]) takes the pillar of the year number given by this table:

SettingDays the stem-branch of year number N covers
Lunar New Year (yearDivide: 'normal', default)Lunar year N, from New Year's Day to New Year's Eve
Start of spring (yearDivide: 'exact')From the start of spring (lichun) in Gregorian year N to the day before the next one

Take 2024: the start of spring falls on 2-4 and Lunar New Year on 2-10. The days 2-4 to 2-9 belong to gui-mao (lunar 2023) under the New Year boundary and to jia-chen (year number 2024) under the start-of-spring boundary. To get the year pillar for a date, settle the year number by the boundary first, then take its pillar.

On this page