(目录)

cron-validator 验证cron表达式

相关文档

安装

npm install cron-validator

代码示例

import { isValidCron } from 'cron-validator'

// 验证成功
if (isValidCron('* * * * *')) {
  // Do something
  console.log(`Running every minute`);
}
// Running every minute

// 验证失败
if (isValidCron('* * * * T')) {
  // Do something
  console.log(`Running every minute for the first time`);
}

cron-parser 解析cron表达式

相关文档

安装

npm install cron-parser

代码示例

import cronParser from 'cron-parser'

// 每分钟执行一次
var interval = cronParser.parseExpression('* * * * *')

// 解析结果
console.log('Date: ', interval.next().toString())
// Date:  Sat Jul 01 2023 17:03:00 GMT+0800 (China Standard Time)
console.log('Date: ', interval.next().toString())
// Date:  Sat Jul 01 2023 17:04:00 GMT+0800 (China Standard Time)
console.log('Date: ', interval.next().toString())
// Date:  Sat Jul 01 2023 17:05:00 GMT+0800 (China Standard Time)