【问题标题】:Every 3 and 6 Months Recurrence on later.js stating from a particular date从特定日期开始在 later.js 上每 3 个月和 6 个月重复一次
【发布时间】:2018-11-16 02:56:19
【问题描述】:

我正在尝试使用 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)

这给出了从当前月份开始的 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)

每年的那个月之后才会出现复发。我希望从特定日期开始重复,这样其他年份的重复不会受到影响。

我的 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);

【问题讨论】:

    标签: javascript node.js laterjs


    【解决方案1】:

    我最初计算出一年中可以有提醒的月份,后来创建了所需年数的 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);
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 2015-10-02
      • 2016-11-14
      • 2023-03-28
      相关资源
      最近更新 更多