Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run cron at last day of the month using node cron

Tags:

node.js

I am using node cron to run the cron every last day of the month

var job = new CronJob('01 01 12 * * *', function() {
    //my function to do job
 }, function () {

 }, true);

so what time I should set so that the script run every last day of the month

like image 872
Irfan Khan Avatar asked Oct 28 '25 08:10

Irfan Khan


1 Answers

The simplest method actually to run the cron at 00:01 the next day of the month, and to use 0 0 1 * * But if you insist on running it on the last day you will need to check in your script.

Use the following js snippet;

const today = new Date();
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

if (today.getMonth() !== tomorrow.getMonth()) {
  // run your cron job
}

so you can program your cron to run daily but the if will guard to execute it only when it is the last day of the month.

like image 85
Alexandru Olaru Avatar answered Oct 30 '25 23:10

Alexandru Olaru