将 cron 转换为“更简单”的人类可读格式的包?

Seb*_*aan 6 javascript cron scheduling package npm

我一直在互联网上寻找可以实现这一目标的 NPM 包,但我还没有找到。

\n

我正在寻找的东西表面上很简单。一个 cron 库,可以将每月的 cron 作业转换为人类可读的文本,同时保持简单。例如:如果我输入0 0 14 * *,我希望它翻译为“每月”。相反,这会转换为“ At\xc2\xa000:00\xc2\xa0on the day-of-month 14”。 ”,这是一个向用户显示的尴尬字符串。

\n

更多的例子是:

\n
    \n
  • 0 8 * * *应该翻译为“每日
  • \n
  • 0 0 10 * *还应该翻译为“每月
  • \n
\n

如果可能的话,我想远离创建自己的专有库。有谁知道是否有任何 JS 包可以满足我的要求?

\n

小智 5

您可以使用https://www.npmjs.com/package/cronstrue

cRonstrue 是一个 JavaScript 库,它解析 cron 表达式并输出人类可读的 cron 计划描述。

cronstrue.toString("* * * * *");
> "Every minute"

cronstrue.toString("0 23 ? * MON-FRI");
> "At 11:00 PM, Monday through Friday"

cronstrue.toString("0 23 * * *", { verbose: true });
> "At 11:00 PM, every day"

cronstrue.toString("23 12 * * SUN#2");
> "At 12:23 PM, on the second Sunday of the month"

cronstrue.toString("23 14 * * SUN#2", { use24HourTimeFormat: true });
> "At 14:23, on the second Sunday of the month"

cronstrue.toString("* * * ? * 2-6/2", { dayOfWeekStartIndexZero: false });
> "Every second, every 2 days of the week, Monday through Friday"

cronstrue.toString("* * * 6-8 *", { monthStartIndexZero: true });
> "Every minute, July through September"
Run Code Online (Sandbox Code Playgroud)