我有一个时区(timerTimeZone):例如“美国/芝加哥”。
让timerTimeZone =“美国/芝加哥”
我们的服务器本地时间是UTC。
我想在存储在timerTimeZone变量中的时区中,每晚12点执行一个函数。
假设程序在UTC时间6.00 PM / CST 1.00 PM运行。因此,第一次执行应该是在11小时(12:00 AM CST)之后,下一次执行每24小时执行一次。
我一直在尝试使用moment和moment-timezone,但找不到任何方法。
发布于 2020-10-23 17:13:00
我建议使用优秀的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")));https://stackoverflow.com/questions/64473195
复制相似问题