0

Here is the code which runs a task every 7 days.

cron.schedule('0 0 */7 * *',()=>{
   console.log('Task running every 7 days')
});

Now I want to run this only after a particular date, say tomorrow. How can I implement it? Can someone help?

loksan
  • 157
  • 3
  • 17

2 Answers2

1

You can use the setTimeout() function to specify when to start the scheduling:

let callback = () => {cron.schedule('0 0 */7 * *',()=>{console.log('Task running every 7 days')})};
setTimeout(callback, delay);
aminits
  • 174
  • 4
1

My approach would be to see what is today's date and if its something you like, start the cron job like so:

const todaysDate = new Date().toJSON().slice(0,10).replace(/-/g,'/'); // gives "2020/10/25";

if(todaysDate === "2020/10/27"){
    // begin the CRON job
}

I obviously made an assumption that you wanted to run this job on 27 Oct 2020

nishkaush
  • 1,512
  • 1
  • 13
  • 20