如何在特定时间之间安排一个任务?

huangapple go评论84阅读模式
英文:

How to schedule a job between specific time?

问题

我正在尝试使用node-cron安排一个工作,它在上午9:00到下午3:30之间每隔10秒运行一次,从周一到周五,但我无法实现它。这是我的Node Cron代码,目前我可以在上午9:00到下午4:00之间安排,但我想要在上午9:00到下午3:30之间安排,我该如何在node-cron中实现这一点?

const job = cron.schedule('*/10 9-15 * * 1-5', () => {
   console.log(new Date());
}, {
timezone: 'Asia/Kolkata'
});
英文:

I'm trying to schedule a job which runs for every 10 seconds between 9:00 AM to 3:30 PM from Monday to Friday using node-cron but I cannout achieve it. Here's my Node Cron code right now I can able to schedule between 9:00 AM to 4:00 PM but I want it from 9:00 AM to 3:30PM, How can I achieve this in node-cron?

   const job = cron.schedule('*/1 9-16 * * 1-5', () => {
      console.log(new Date());
   }, {
    timezone: 'Asia/Kolkata'
   });

答案1

得分: 1

以下是翻译好的部分:

CRON 1:

  • */10:每10秒
  • *:每分钟
  • 9-15:从第9小时(09:00 AM)到第15小时(03:00 PM)
  • *:每天
  • *:每月
  • 1-5:从周一到周五

CRON 2:

  • */10:每10秒
  • 0-30:从第0分钟到第30分钟
  • 15:在第15小时(03:00 PM)
  • *:每天
  • *:每月
  • 1-5:从周一到周五
英文:

Following @ashish singh's answer, use two cron jobs:

const cron = require('node-cron')

const job = () => {
  console.log(new Date())
}

// Each 10 seconds past every hour from 9 through 15 on every day-of-week from Monday through Friday
cron.schedule('*/10 * 9-15 * * 1-5', () => job())

// Each 10 seconds from 0 through 30 past hour 15 on every day-of-week from Monday through Friday
cron.schedule('*/10 0-30 15 * * 1-5', () => job())

CRON 1:

  • */10: Each 10 seconds
  • *: Every minute
  • 9-15: From hour 9 (09:00 AM) to 15 (03:00 PM)
  • *: Every day
  • *: Every month
  • 1-5: From Monday to Friday

CRON 2:

  • */10: Each 10 seconds
  • 0-30: From minute 0 to 30
  • 15: At hour 15 (03:00 PM)
  • *: Every day
  • *: Every month
  • 1-5: From Monday to Friday

* Node CRON documentation here.

答案2

得分: 0

simpler way seems to be using two scheduler

  1. one for 9 to 3 '0 9-15 * * *' ( this is for only min and hour )
  2. one for 3 to 3.30 '30 15 * * *' ( this is only for minute and hour)
英文:

simpler way seems to be using two scheduler

  1. one for 9 to 3 '* 9-15' ( this is for only min and hour )
  2. one for 3 to 3.30 '0-30 15' ( this is only for minute and hour)

huangapple
  • 本文由 发表于 2023年1月9日 20:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75057202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定