# 历法查询 (/zh/docs/js/calendar)

农历年的月份表、公历与农历互换、农历年的年干支，与排盘同一份历法内核。



不排盘直接查历法：某个农历年有哪些月、各月大小、闰哪个月，公历与农历互换，农历年的年干支。
与排盘同一份历法内核（逐函数移植自 lunar-typescript），结果与星盘的 `rawDates.lunarDate`、年柱逐日一致，
由 lunar-typescript 生成的金标按农历 1–9999 年逐日对照。

支持公历与农历 1–9999 年。1582-10-15 以前按儒略历，1582-10-05 至 10-14 不存在。

<Callout title="报错">
  给出完整日期的换算（`calendar.solarToLunar`、`calendar.lunarToSolar`）在日期不存在时报 `IztroError`（code 为 `invalid_date`），与排盘入口相同；
  只给年份的查询（`calendar.lunarMonths`、`calendar.lunarYearPillar`）在年份越界时报 `IztroError`（code 为 `invalid_argument`），与按农历年取运限相同。
</Callout>

```js
import { calendar } from 'x-iztro';
```

***

## calendar.lunarMonths [#calendarlunarmonths]

**用途**　取农历某年的全部月份：月份、是否闰月、天数（大小月）与初一的公历日期。

**签名**

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

**参数**

| 参数     | 类型       | 必填 | 默认 | 说明         |
| ------ | -------- | -- | -- | ---------- |
| `year` | `number` | 是  | —  | 农历年，1–9999 |

**返回值**　`LunarMonthInfo[]`，按时间先后排列，闰月紧跟在所闰的月之后。每项：

| 字段         | 类型             | 说明                                          |
| ---------- | -------------- | ------------------------------------------- |
| `month`    | `number`       | 月份 1–12；闰月取所闰的月份，闰四月为 4                     |
| `isLeap`   | `boolean`      | 是否闰月                                        |
| `dayCount` | `number`       | 天数：29（小月）或 30（大月）                           |
| `firstDay` | `RawSolarDate` | 初一的公历日期：`solarYear`、`solarMonth`、`solarDay` |

**示例**　2023 年闰二月：

```js
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}`);
}
```

**输出**

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

**边界与陷阱**

<Accordions>
  <Accordion title="不一定是 12 或 13 个月">
    个别古代年份与 lunar-typescript 一致地少一个月：农历 237 年只有 11 个月、没有三月。
    按 1–12 循环取月份时以返回的列表为准。
  </Accordion>

  <Accordion title="农历 9999 年腊月的后 27 天在公历 10000 年">
    农历 9999 年腊月初一是 9999-12-30，各月初一都在公历 9999 年内，`firstDay.solarYear` 不会是 10000；
    腊月初三至廿九这 27 天落在公历 10000 年，`calendar.lunarToSolar` 对它们返回的 `solarYear` 为 10000。
    这些日子不能再交给只收公历 1–9999 年的入口（排盘、`calendar.solarToLunar`）。
  </Accordion>
</Accordions>

***

## calendar.solarToLunar [#calendarsolartolunar]

**用途**　公历日期换农历日期。

**签名**

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

**参数**

| 参数      | 类型       | 必填 | 默认 | 说明         |
| ------- | -------- | -- | -- | ---------- |
| `year`  | `number` | 是  | —  | 公历年，1–9999 |
| `month` | `number` | 是  | —  | 公历月，1–12   |
| `day`   | `number` | 是  | —  | 公历日        |

**返回值**　`RawLunarDate`：`lunarYear`、`lunarMonth`（1–12）、`lunarDay`、`isLeap`，与星盘的 `rawDates.lunarDate` 同一类型。

**示例**　2023-3-22 是闰二月初一：

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

**输出**

```text
2023 2 1 true
```

**边界与陷阱**

<Accordions>
  <Accordion title="公历 1 年年初属农历 0 年">
    公历 1 年 1 月初的几十天还在农历 0 年的冬月、腊月，返回的 `lunarYear` 为 0；
    这个 0 不能再交给只收 1–9999 年的农历入参。
  </Accordion>

  <Accordion title="改历空缺">
    1582-10-05 至 10-14 不存在（儒略历 10 月 4 日的次日是格里历 10 月 15 日），传入即报 `IztroError`（code 为 `invalid_date`）。
  </Accordion>
</Accordions>

***

## calendar.lunarToSolar [#calendarlunartosolar]

**用途**　农历日期换公历日期。

**签名**

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

**参数**

| 参数               | 类型        | 必填 | 默认      | 说明                |
| ---------------- | --------- | -- | ------- | ----------------- |
| `year`           | `number`  | 是  | —       | 农历年，1–9999        |
| `month`          | `number`  | 是  | —       | 农历月，1–12；闰月写所闰的月份 |
| `day`            | `number`  | 是  | —       | 农历日，不超过当月天数       |
| `options.isLeap` | `boolean` | 否  | `false` | 这一天在闰月里           |

**返回值**　`RawSolarDate`：`solarYear`、`solarMonth`、`solarDay`。

**示例**

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

// 2024 年没有闰二月：报错，不退回常规的二月
try {
  calendar.lunarToSolar(2024, 2, 1, { isLeap: true });
} catch (e) {
  console.log(e.code);
}
```

**输出**

```text
2023-3-22
invalid_date
```

**边界与陷阱**

<Accordions>
  <Accordion title="没有这个闰月就报错">
    `options.isLeap` 为真而该年不闰这个月时报 `IztroError`（code 为 `invalid_date`）。排盘入口（`astro.byLunar` 的 `isLeapMonth`，与 iztro 相同）
    在这种情况下忽略闰月标记、按常规月排盘；历法换算不这样做，因为退回常规月得到的日期差一个月，却看不出错。
  </Accordion>

  <Accordion title="没有 fix_leap">
    排盘的 `fixLeap`（闰月十五之后视作次月）管的是星曜按哪个月安，不改变这一天是公历哪天，
    所以这里只有「是否闰月」一个参数。
  </Accordion>
</Accordions>

***

## calendar.lunarYearPillar [#calendarlunaryearpillar]

**用途**　农历某年的年干支，返回天干与地支的语言无关标识。

**签名**

```ts
calendar.lunarYearPillar(year: number): Pillar
```

**参数**

| 参数     | 类型       | 必填 | 默认 | 说明        |
| ------ | -------- | -- | -- | --------- |
| `year` | `number` | 是  | —  | 年号，1–9999 |

**返回值**　`Pillar`（`[天干标识, 地支标识]`）。公历 4 年为甲子，此后按年号循环。

**示例**

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

**输出**

```text
jiaHeavenly chenEarthly
```

**分界口径**　年干支只由年号决定，与 `year_divide` 无关；分界口径决定的是这一柱从哪天起算。
排盘的年柱（`rawDates.chineseDate.pillarKeys()[0]`）按下表取对应年号的这一柱：

| 口径                                | 年号 N 的年干支管哪些日子     |
| --------------------------------- | ------------------ |
| 正月初一分界（`yearDivide: 'normal'`，默认） | 农历 N 年正月初一至除夕      |
| 立春分界（`yearDivide: 'exact'`）       | 公历 N 年立春当日至次年立春前一日 |

以 2024 年为例：立春在 2-4，正月初一在 2-10。2-4 至 2-9 这几天正月初一分界下属癸卯（农历 2023 年），
立春分界下属甲辰（年号 2024）。按日期取年柱时，先按口径定出年号，再取这一柱。
