你如何计算过去的 cron 作业时间表?

How do you calculate past cron jobs schedules?

我希望能够在给定 cron 字符串的情况下找到 cron 作业 运行 的时间。例如,对于 0 */5 * * * *,作业每五分钟运行一次。如果是 13:02,则下一个作业在 13:05,最后一个作业在 13:00。如果有图书馆可以做到这一点,我想尽可能使用它。

查看 Later.js

来自文档:

// fires at 10:15am every day
var cron = '0 */5 * * * *';
var s = later.parse.cron(cron);

later.schedule(s).next(10);
// gets you the next 10 times the cron expression will run

later.schedule(s).prev(10);
// gets you the last 10 times the cron expression ran

我会考虑使用 cron-parser,这允许您从 cron 表达式确定之前和未来的 cron 时间。

例如:

const parser = require('cron-parser');

const options = {
  currentDate: new Date('2020-06-25T13:02:00Z'),
  iterator: true
};

const interval = parser.parseExpression('0 */5 * * * *', options);

console.log("Previous run:", interval.prev().value.toISOString());
console.log("Next run:", interval.next().value.toISOString());

您应该会看到如下内容:

 Previous run: 2020-06-25T13:00:00.000Z
 Next run: 2020-06-25T13:05:00.000Z