loksan
loksan

Reputation: 177

Node cron running for every 7 days after a particular date

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?

Upvotes: 0

Views: 2971

Answers (2)

nishkaush
nishkaush

Reputation: 1548

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

Upvotes: 1

aminits
aminits

Reputation: 184

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

Upvotes: 1

Related Questions