在不同于服务器时区UTC的时区CST中的每个午夜执行功能

问题描述

我有一个时区(timerTimeZone):例如“美国/芝加哥”。

let timerTimeZone =“美国/芝加哥”

我们的服务器本地时间为世界标准时间。

我想每天晚上12:00在时区中执行一个函数,该时区存储在timerTimeZone变量中。

让我们说程序在UTC下午6.00 / CST下午1:00运行。因此,第一次执行应该在11小时(美国中部标准时间12.00 AM)之后执行,然后在以后的每24小时执行一次。

我一直在尝试使用矩和矩时区,但找不到任何方法。

解决方法

我建议使用出色的Cron模块。

这允许您根据cron表达式安排任务,还可以指定要使用的IANA时区。

我还在此处记录了作业将在指定的时区和UTC中运行的下5个日期。

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

// Run at midnight every night
const cronExpression = "00 00 * * *";

const timeZone = "America/Chicago";

const cronJob = new CronJob(
    cronExpression,cronFunction,null,true,timeZone
);

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

// Get the next N dates the job will fire on...
const nextDates = cronJob.nextDates(5);
console.log(`Next times (${timeZone}) the job will run on:`,nextDates.map(d => d.tz(timeZone).format("YYYY-MM-DD HH:mm")));
console.log("Next times (UTC) the job will run on:",nextDates.map(d => d.utc().format("YYYY-MM-DD HH:mm")));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...