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 minute9-15
: From hour 9 (09:00 AM) to 15 (03:00 PM)*
: Every day*
: Every month1-5
: From Monday to Friday
CRON 2:
*/10
: Each 10 seconds0-30
: From minute 0 to 3015
: At hour 15 (03:00 PM)*
: Every day*
: Every month1-5
: From Monday to Friday
* Node CRON documentation here.
CodePudding user response:
simpler way seems to be using two scheduler
- one for 9 to 3
'* 9-15'
( this is for only min and hour ) - one for 3 to 3.30
'0-30 15'
( this is only for minute and hour)