每 3 个月和 6 个月重复一次.js说明从特定日期开始



我正在尝试使用后者.js(https://github.com/bunkat/later(每 3 个月和 6 个月创建一次重复周期。

这是我的代码

// My value.scheduled_date is 2018-09-06
var d = new Date(value.scheduled_date);
var day = d.getDate();
// month count will be -1 as it starts from 0
var month = d.getMonth();
var year = d.getFullYear();
var recurSched = later.parse.recur().on(day).dayOfMonth().every(3).month();
var schedules = later.schedule(recurSched).next(5);
console.log(schedules)

这给出了从当前月份开始的 3 个月重复,但我希望从scheduled_date开始重复。当我将开始 On 添加到代码中时

var recurSched = later.parse.recur().on(day).dayOfMonth().every(3).month().startingOn(month + 1);
var schedules = later.schedule(recurSched).next(5);
console.log(schedules)

每年的复发仅在该月之后盯着。我希望重复周期从特定日期开始,这样其他年份的重复周期就不会受到影响。

我的 6 个月代码

也是如此
var recurSched = later.parse.recur().on(day).dayOfMonth().every(6).month().startingOn(month+1);
var schedules = later.schedule(recurSched).next(5);
console.log(schedules);

我最初计算出一年中可以提醒的月份,后来创建了所需年数的 cron。例如;如果我们在 2018 年 1 月 1 日创建季度提醒,它将在 4 月、7 月、10 月和 1 月重复。 上述代码如下

var recurSched = "";
var next_scheduled_date = "";
var next_scheduled_day = "";
var next_scheduled_month = "";
var next_scheduled_year = "";
var list_of_months = [];
for (var i = 1; i <= 2; i++) {
var next_scheduled_date = new Date(value.scheduled_date);
next_scheduled_date = new Date(next_scheduled_date.setMonth(next_scheduled_date.getMonth() + parseInt(i*6)));
var next_scheduled_day = next_scheduled_date.getDate();
var next_scheduled_month = next_scheduled_date.getMonth();
var next_scheduled_year = next_scheduled_date.getFullYear();
list_of_months.push(parseInt(next_scheduled_month + 1))
};
list_of_months.sort(function(a, b){return a - b});

请注意,我们还需要按升序对月份进行排序。递归的代码如下

var recurSched = later.parse.recur().on(list_of_months[0],list_of_months[1]).month().on(day).dayOfMonth();
var schedules = later.schedule(recurSched).next(4);

如果你想独立有效地运行循环函数,最好使用系统的调度程序,即Linux有crontab 你可以在这里参考crontab。此外,您可以参考 crontab 大师的多种模式。例:

0 9 * 3,6,9,12 * node script.js

这将在 3 月、6 月、9 月和 12 月的 09:00 运行您的脚本.js。

最新更新