Running cron job at only specific date and time

为君一笑 提交于 2020-04-17 22:40:51

问题


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 2018-02-22@12.00 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?


回答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');
});



回答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.');
});


来源:https://stackoverflow.com/questions/48274326/running-cron-job-at-only-specific-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!