node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

How to create schedule from seconds?

Open Colman opened this issue 4 years ago • 0 comments

I want to create a function such that given an integer, I get a cron job to run every time the UNIX timestamp is divisible by that integer. This is what I have so far:

function secToCron(sec) {
  if (sec <= 60) {
    return "*/" + sec + " * * * * *";
  } else if (sec <= 3600) {
    return "* */" + sec / 60  + "* * * *";
  } else if (sec <= 86400) {
    return "* * */" + sec / 3600  + "* * *";
  } 
}

However, the issue here is that if sec = 100, it will give me a decimal in the string. Also this doesn't work for anything beyond 1 day because there is a variable number of days in a month. How can I solve this?

Thanks

Colman avatar Aug 07 '20 06:08 Colman