Home > Software engineering >  How to schedule a job between specific time?
How to schedule a job between specific time?

Time:01-10

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'
   });

CodePudding user response:

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.

CodePudding user response:

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)
  • Related