@cheap-glitch/mi-cron

中文文档
2.0.0发布于 2025/04/18ISCTypeScriptES Module零依赖近 30 天下载 1.3万

A microscopic parser for standard cron expressions.

原始使用文档
文档目录10 节
  1. Features
  2. Installation
  3. Usage
  4. API
  5. parseCron(exp: string): CronSchedule
  6. parseCron.nextDate(exp: string | CronSchedule[, from: Date …
  7. Changelog
  8. Contributing
  9. Related
  10. License

📆 mi-cron

License Latest release Coverage status

mi-cron is a microscopic (~1KB minified & gzipped) parser for standard cron expressions.

Features

  • Supports the complete standard cron syntax
  • Supports some convenient syntax extensions: @-shorthands (@daily) and steps (*/10)
  • Can compute the next scheduled date for a given expression
  • Tiny & dependency-free

Installation

npm i @cheap-glitch/mi-cron

Usage

const { parseCron } = require('@cheap-glitch/mi-cron');

console.log(parseCron.nextDate('*/5 6-12 3 3 *').toUTCString());
// Wed, 03 Mar 2021 06:00:00

API

parseCron(exp: string): CronSchedule

Parses a standard cron expression. Supports:

  • globs (*)
  • ranges (0-30, mon-fri)
  • steps (*/3, 20-31/2, 10/5)
  • lists (1,15, 0-10,20-30/2)
  • @-shorthands (@weekly)

Does NOT support:

  • L, W, #, ?, H
  • year field
  • @reboot

Returns an object with all possible values for each field (minutes, hours, days, months and days of the week), or undefined if the expression is invalid (wrong syntax, unsupported instruction, impossible range, etc).

const { parseCron } = require('@cheap-glitch/mi-cron');

console.log(parseCron('*/5 6-10 1,15 * wed'));
// {
// 	minutes:  [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
// 	hours:    [6, 7, 8, 9, 10],
// 	days:     [1, 15],
// 	months:   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
// 	weekDays: [3],
// }

parseCron.nextDate(exp: string | CronSchedule[, from: Date = new Date.now()]): Date

Takes a cron schedule or expression and returns the next date that matches the schedule, or undefined if the expression is invalid. If given a datetime as the second argument, it will start the computation from this time (otherwise it will use the current datetime at the moment it's called).

const { parseCron } = require('@cheap-glitch/mi-cron');

console.log(parseCron.nextDate('* * * * *', new Date('01 Jan 2020 00:00:00 GMT')).toUTCString());
// Wed, 01 Jan 2020 00:01:00

// Get the next five scheduled dates
const schedule = parseCron('@weekly');
const nextDate = new Date();
for (let i=0; i<5; i++) {
    console.log(nextDate = parseCron.nextDate(schedule, nextDate));
}

Changelog

See the full changelog here.

Contributing

Contributions are welcomed! Please open an issue before submitting substantial changes.

License

ISC

版本与发布记录

@cheap-glitch/mi-cron 共发布 3 个版本,首次发布于 2020/09/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2025/04/181.6 年前
1.0.12020/09/306 年前
1.0.02020/09/286 年前

关于 @cheap-glitch/mi-cron 的常见问题

@cheap-glitch/mi-cron 是什么?

A microscopic parser for standard cron expressions.

如何安装 @cheap-glitch/mi-cron?

在项目目录运行 npm install @cheap-glitch/mi-cron,或使用 pnpm add / yarn add / bun add。

@cheap-glitch/mi-cron 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cheap-glitch/mi-cron 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cheap-glitch/mi-cron 可以商用吗?

@cheap-glitch/mi-cron 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cheap-glitch/mi-cron 的下载量有多少?

根据 npm 官方下载统计,@cheap-glitch/mi-cron 在 2026/10/10 – 2026/11/08 这 30 天内被下载 13,440 次,日均约 448 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cheap-glitch/mi-cron 还在维护吗?

最新版本 2.0.0 发布于 2025/04/18(1.6 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/09/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。