首页 开发文档 农历公历互转

农历公历互转

公历与农历双向转换。公历转农历返回农历年月日、中文农历、干支年/月/日、生肖、星期与当日节气;农历转公历返回对应公历日期。基于国产农历算法库本地计算,覆盖范围 1900-2100 年,不依赖任何第三方接口。

请求信息
请求地址 https://cx4.cn/openApi/data/lunar 复制
请求方式 GET / POST
接口标识 data_lunar
鉴权方式 必须
四种方式任选其一:Header「X-Api-Key」/ Header「Authorization: Bearer 你的key」/ URL 参数「?key=你的key」/ POST 参数「key」
请求参数
参数名 类型 必填 说明 示例
action string 否 转换方向:solar2lunar 公历转农历 / lunar2solar 农历转公历 solar2lunar
date string 否 公历日期 YYYY-MM-DD,action=solar2lunar 时生效,留空或传 today 取当天 2026-10-03
year int 否 农历年 1900-2100,action=lunar2solar 时必填 2026
month int 否 农历月 1-12,闰月用负数表示(如 -4 为闰四月) 8
day int 否 农历日 1-30 23
返回参数

下表字段位于返回结构的 data 内;外层 code / message 为全平台统一格式,不在此重复列出。

参数名 类型 说明 示例
ok bool 是否成功 true
action string 转换方向 solar2lunar
solar string 公历日期 YYYY-MM-DD 2026-10-03
lunar string 农历中文全称 二〇二六年八月廿三
lunar_ymd string 农历年月日 2026-8-23
lunar_month_cn string 农历月中文 八
lunar_day_cn string 农历日中文 廿三
ganzhi_year string 年干支 丙午
ganzhi_month string 月干支 丁酉
ganzhi_day string 日干支 庚戌
shengxiao string 生肖 马
week string 星期几 六
jieqi string 当日节气,无则为空
返回示例

成功

{
    "code": 200,
    "message": "操作成功",
    "data": {
        "ok": true,
        "action": "solar2lunar",
        "solar": "2026-10-03",
        "lunar": "二〇二六年八月廿三",
        "lunar_ymd": "2026-8-23",
        "lunar_month_cn": "八",
        "lunar_day_cn": "廿三",
        "ganzhi_year": "丙午",
        "ganzhi_month": "丁酉",
        "ganzhi_day": "庚戌",
        "shengxiao": "马",
        "week": "六",
        "jieqi": ""
    }
}

失败

{
    "code": 400,
    "message": "日期格式有误,请使用 YYYY-MM-DD",
    "data": null
}
平台通用状态码
状态码 说明
200调用成功
400缺少必填参数或参数格式有误
401密钥缺失或无效
402余额不足,请充值后重试
403未开通该接口,请在本页开通后调用
404接口不存在或已下架
429触发限流,请稍后重试
500接口调用失败(已扣款的会自动退款)
调用示例

cURL 复制

curl -X POST "https://cx4.cn/openApi/data/lunar" -H "X-Api-Key: 你的SecretKey" -d "action=solar2lunar&date=2026-10-03&year=2026&month=8&day=23"

JavaScript 复制

fetch('https://cx4.cn/openApi/data/lunar', {
  method: 'POST',
  headers: {
    'X-Api-Key': '你的SecretKey',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'action=solar2lunar&date=2026-10-03&year=2026&month=8&day=23'
})
.then(res => res.json())
.then(data => console.log(data));

PHP 复制

post('https://cx4.cn/openApi/data/lunar', [
    'headers' => ['X-Api-Key' => '你的SecretKey'],
    'form_params' => ['action' => 'solar2lunar', 'date' => '2026-10-03', 'year' => '2026', 'month' => '8', 'day' => '23'],
]);
echo $response->getBody();

Java 复制

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://cx4.cn/openApi/data/lunar"))
    .header("X-Api-Key", "你的SecretKey")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString("action=solar2lunar&date=2026-10-03&year=2026&month=8&day=23"))
    .build();
HttpResponse response = client.send(
        request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Python 复制

import requests

res = requests.post('https://cx4.cn/openApi/data/lunar',
    headers={'X-Api-Key': '你的SecretKey'},
    data={'action': 'solar2lunar', 'date': '2026-10-03', 'year': '2026', 'month': '8', 'day': '23'})
print(res.json())
在线测试 (使用你的密钥真实调用,计费与正常调用一致)

登录后可在线测试: 立即登录

计费说明

¥0.0010

每次调用


每日免费额度:100 次

累计调用:8 次


快速上手

1. 注册并完成邮箱验证:立即注册

2. 登录后复制 Secret Key:去登录

3. 点击右侧「开通接口」按钮

4. 用下方「在线测试」一键体验,或复制下方示例代码直接调用