DEV Community

Discussion on: Scheduling tasks in NodeJS with cron job

Collapse
 
zt4ff_1 profile image
Kayode • Edited

There's no single cron notation to run a task every last day of the month. You can run the task at the first day of the month at midnight or make use of npm module like bree to run the task at the last day of the month or have mutiple cron jobs for a single task.

But if you want to stick to cron, a simple solution is to run the tasks from 28 - 31 every month but make use of any robust date framework (that what date tomorrow is) to determine if any of the date is the last date. For instance

cron.schedule("0 0 28-31 * *", () => {
  if (date.getTomorrow() ===  "1") {
    // that's the last day
   doSomeTasks()
  } 
})
Enter fullscreen mode Exit fullscreen mode

Some comments have been hidden by the post's author - find out more