Hedi
Hedi

Reputation: 322

node-schedule is not work at time

i want to use node-schedule , i get information from Data Base every day , and for each item i want to do some thing at special time. this is my code :

users.forEach(function(users_entry){
                                if(err){
                                    console.log(err);
                                }
                                else{
                                   var date = new Date(2014, 11, 29, 11, 45, 0);
                                   schedule.scheduleJob(date,
                                   function(){
                                      console.log('The world is going to end today.');
                                   });
                                 }
                          });

but above code doesn't run at mentioned time and works all the time. what is problem?

Upvotes: 6

Views: 8678

Answers (3)

Ajay Kakde
Ajay Kakde

Reputation: 53

Try this:

const schedule = require('node-schedule');
const moment = require('moment');

const rule = scheduled.RecurrenceRule();

let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
     console.log("Scheduled")
});

Upvotes: 0

kumbhani bhavesh
kumbhani bhavesh

Reputation: 2257

try this

var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 1-7', function() {
/*
 * Runs every day
 * at 12:00:00 AM.
*/
}, function () {
 /* This function is executed when the job stops */
},
 true, /* Start the job right now */
 timeZone /* Time zone of this job. */
);

Upvotes: 0

Hedi
Hedi

Reputation: 322

i changed my code and used cron. https://www.npmjs.org/package/cron

it works very well :)

var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function(){
    console.log('You will see this message every second');
}, null, true, "America/Los_Angeles");

Upvotes: 7

Related Questions