Running cron job at only specific date and time

10,632

Solution 1

for specific date, you could use something like following as provided in example

var schedule = require('node-schedule');
var date = new Date(2018, 1, 22, 12, 0, 0);

var j = schedule.scheduleJob(date, function(){
  console.log('job is running');
});

Solution 2

You can pass date object in it. if you read document further you will find it here : https://www.npmjs.com/package/node-schedule#date-based-scheduling

var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);

var j = schedule.scheduleJob(date, function(){
 console.log('The world is going to end today.');
});
Share:
10,632

Related videos on Youtube

shellakkshellu
Author by

shellakkshellu

stay hungry stay foolish

Updated on June 04, 2022

Comments

  • shellakkshellu
    shellakkshellu almost 2 years

    In our express js application have a feature in admin module that he can send mail to users at specific dates (he can able to select a specific date and time).

    Say if the date and time is [email protected] we need to run the email code at that time.

    I think this package will full fill our needs https://www.npmjs.com/package/node-schedule,

    Here is their documentation

    *    *    *    *    *    *
    ┬    ┬    ┬    ┬    ┬    ┬
    │    │    │    │    │    │
    │    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
    │    │    │    │    └───── month (1 - 12)
    │    │    │    └────────── day of month (1 - 31)
    │    │    └─────────────── hour (0 - 23)
    │    └──────────────────── minute (0 - 59)
    └───────────────────────── second (0 - 59, OPTIONAL)
    

    But it doesn't say how to run on specific date (year field is missing??) so how do i achieve my need?

  • shellakkshellu
    shellakkshellu over 6 years
    Thanks for the answer one more question do i need to run this cron on separate express js application??
  • Sudhir Bastakoti
    Sudhir Bastakoti over 6 years
    @iambatman creating a separate file in your express application would be a better option, so that this file only consists your cron related tasks
  • Gilad Barner
    Gilad Barner over 3 years
    Nice. Careful though: the second argument of the months is a zero-based index, i.e. 0 = January, 11 = December. I fell into that pit now...
  • Gilad Barner
    Gilad Barner over 3 years
    Nice. Careful though: the second argument of the months is a zero-based index, i.e. 0 = January, 11 = December. I fell into that pit now...
  • Dyary
    Dyary over 3 years
    thanks . that saved me , i didn't think it was zero based index
  • hardik chugh
    hardik chugh over 2 years
    I wont recommend to go with this package, since lets say you schedule job and you restart sever after that, then your job wont run on that time you specified earlier.
  • Liviu
    Liviu over 2 years
    @hardikchugh what package do you recomment?
  • hardik chugh
    hardik chugh over 2 years
    Node-cron is fine npmjs.com/package/node-cron