仅在特定日期和时间运行cron作业 [英] Running cron job at only specific date and time
问题描述
在我们的express js应用程序中,管理模块具有一项功能,他可以在特定日期向用户发送邮件(他可以选择特定的日期和时间)。
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).
假设日期和时间为2018-02-22@12.00,那么我们需要在该时间运行电子邮件代码。
Say if the date and time is 2018-02-22@12.00 we need to run the email code at that time.
我认为此软件包将完全填满我们的需要 https://www.npmjs.com/package/node-schedule ,
I think this package will full fill our needs https://www.npmjs.com/package/node-schedule,
这是他们的文档
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ 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)
但这不是y如何在特定日期运行(缺少年份字段?),那么如何实现我的需要?
But it doesn't say how to run on specific date (year field is missing??) so how do i achieve my need?
推荐答案
针对特定日期日期,您可以使用示例中提供的以下内容
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');
});
这篇关于仅在特定日期和时间运行cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!