Ara*_*vas 5 javascript node.js laterjs
我正在尝试使用 later.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)
Run Code Online (Sandbox Code Playgroud)
这给出了从当月开始的 3 个月重复,但我希望重复从 schedule_date 开始。当我在代码中添加开始时
var recurSched = later.parse.recur().on(day).dayOfMonth().every(3).month().startingOn(month + 1);
var schedules = later.schedule(recurSched).next(5);
console.log(schedules)
Run Code Online (Sandbox Code Playgroud)
每年只在那个月之后复发。我希望重复从特定日期开始,这样其他年份的重复就不会受到影响。
我的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);
Run Code Online (Sandbox Code Playgroud)
小智 4
我最初计算出一年中可以有提醒的月份,然后根据所需的年数创建了 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});
Run Code Online (Sandbox Code Playgroud)
请注意,我们还需要按升序对月份进行排序。递归的代码如下
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
453 次 |
| 最近记录: |