【问题标题】:Cron - Run every year once at January 1 00:00 or 12.00 amCron - 每年 1 月 1 日 00:00 或 12:00 am 运行一次
【发布时间】:2021-01-06 18:46:49
【问题描述】:

我正在使用 npm i cron 在 node.js 中运行 cron 计划。我想在每年年初的 12.00 am 运行时间表,即每年 1 月 1 日的 12.00 am 一次。我怎样才能让它运行?任何人都可以帮忙。

【问题讨论】:

标签: node.js cron


【解决方案1】:

您应该能够使用下面的代码来执行此操作。当脚本启动时,它将打印出作业将运行的下 10 个日期:

const CronJob = require("cron").CronJob;

const cronExpression ="0 0 1 JAN *";

const cronJob = new CronJob(
    cronExpression,
    cronFunction
);

function cronFunction() {
    console.log("cronFunction: Running....");
    // Add whatever you wish here...
}

// Print out the next dates the job will run
const nextDates = cronJob.nextDates(10);
console.log("Next dates the job will run on:", nextDates.map(d => d.format("YYYY-MM-DD HH:mm")));

cronJob.start();

输出应该是这样的:

Next dates the job will run on: [
  '2022-01-01 00:00',
  '2023-01-01 00:00',
  '2024-01-01 00:00',
  '2025-01-01 00:00',
  '2026-01-01 00:00',
  '2027-01-01 00:00',
  '2028-01-01 00:00',
  '2029-01-01 00:00',
  '2030-01-01 00:00',
  '2031-01-01 00:00'
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多