javascriptnode.jsschedulernode-cron

How to schedule a job between specific time?


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

Solution

  • 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:

    CRON 2:

    * Node CRON documentation here.