首页 开发文档 Cron 表达式解析

Cron 表达式解析

校验并解析 Cron 表达式,返回接下来若干次执行时间(含时间戳与星期)、上一次执行时间、当前时刻是否命中,并给出中文含义描述与字段拆解。支持 5 段与 6 段(带年)表达式、步长、区间、列表、L/W/# 等常见语法,也可指定时区。适合做定时任务配置页的实时预览与合法性校验。

请求信息
请求地址 https://cx4.cn/openApi/dev/cron 复制
请求方式 GET / POST
接口标识 dev_cron
鉴权方式 必须
四种方式任选其一:Header「X-Api-Key」/ Header「Authorization: Bearer 你的key」/ URL 参数「?key=你的key」/ POST 参数「key」
请求参数
参数名 类型 必填 说明 示例
expression string 是 Cron 表达式,5 段(分 时 日 月 周)或 6 段(带年) 0 9 * * *
count int 否 返回未来执行次数 1-20 3
base string 否 起始时间,支持 now 或任意日期时间字符串 now
timezone string 否 时区,如 Asia/Shanghai,留空取系统默认 Asia/Shanghai
explain int 否 是否返回中文描述与字段拆解 0/1 1
返回参数

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

参数名 类型 说明 示例
ok bool 是否成功 true
expression string 原表达式 0 9 * * *
is_valid bool 表达式是否合法 true
is_due bool 起始时刻是否命中 false
timezone string 时区 Asia/Shanghai
next array 未来执行时间列表 []
next[].datetime string 执行时间 2026-10-04 09:00:00
next[].timestamp int 时间戳 1791075600
next[].weekday string 星期 星期日
previous object 上一次执行时间 {}
description string 中文含义描述 每天 09:00 执行
fields array 五个字段拆解 []
返回示例

成功

{
    "code": 200,
    "message": "操作成功",
    "data": {
        "ok": true,
        "expression": "0 9 * * *",
        "is_valid": true,
        "is_due": false,
        "base": "now",
        "timezone": "Asia/Shanghai",
        "next": [
            {
                "datetime": "2026-10-04 09:00:00",
                "timestamp": 1791075600,
                "weekday": "星期日"
            },
            {
                "datetime": "2026-10-05 09:00:00",
                "timestamp": 1791162000,
                "weekday": "星期一"
            },
            {
                "datetime": "2026-10-06 09:00:00",
                "timestamp": 1791248400,
                "weekday": "星期二"
            }
        ],
        "previous": {
            "datetime": "2026-10-03 09:00:00",
            "timestamp": 1790989200,
            "weekday": "星期六"
        },
        "description": "每天 09:00 执行",
        "fields": [
            {
                "field": "minute",
                "name": "分钟",
                "value": "0"
            },
            {
                "field": "hour",
                "name": "小时",
                "value": "9"
            },
            {
                "field": "day",
                "name": "日",
                "value": "*"
            },
            {
                "field": "month",
                "name": "月",
                "value": "*"
            },
            {
                "field": "weekday",
                "name": "星期",
                "value": "*"
            }
        ]
    }
}

失败

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

cURL 复制

curl -X POST "https://cx4.cn/openApi/dev/cron" -H "X-Api-Key: 你的SecretKey" -d "expression=0 9 * * *&count=3&base=now&timezone=Asia/Shanghai&explain=1"

JavaScript 复制

fetch('https://cx4.cn/openApi/dev/cron', {
  method: 'POST',
  headers: {
    'X-Api-Key': '你的SecretKey',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'expression=0 9 * * *&count=3&base=now&timezone=Asia/Shanghai&explain=1'
})
.then(res => res.json())
.then(data => console.log(data));

PHP 复制

post('https://cx4.cn/openApi/dev/cron', [
    'headers' => ['X-Api-Key' => '你的SecretKey'],
    'form_params' => ['expression' => '0 9 * * *', 'count' => '3', 'base' => 'now', 'timezone' => 'Asia/Shanghai', 'explain' => '1'],
]);
echo $response->getBody();

Java 复制

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://cx4.cn/openApi/dev/cron"))
    .header("X-Api-Key", "你的SecretKey")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString("expression=0 9 * * *&count=3&base=now&timezone=Asia/Shanghai&explain=1"))
    .build();
HttpResponse response = client.send(
        request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Python 复制

import requests

res = requests.post('https://cx4.cn/openApi/dev/cron',
    headers={'X-Api-Key': '你的SecretKey'},
    data={'expression': '0 9 * * *', 'count': '3', 'base': 'now', 'timezone': 'Asia/Shanghai', 'explain': '1'})
print(res.json())
在线测试 (使用你的密钥真实调用,计费与正常调用一致)

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

计费说明

¥0.0010

每次调用


每日免费额度:100 次

累计调用:8 次


快速上手

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

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

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

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