5

I am trying to create recurrence every 3 and 6 months using the later.js(https://github.com/bunkat/later).

This is my code

// 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)

This gives 3 months recurrence starting from the current month, but I want the recurrence to start from the scheduled_date. When I add starting On to the code

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

Recurrence is staring only after that month for every year. I want recurrence to start from a particular date so that the recurrence of other years won't be affected.

The same is the case with my 6 months code

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

2 Answers2

4

I initially figured out the months in a year which can have reminders and later created the cron for the number of years required. For example; if we are creating a quarterly reminder on Jan 01,2018 it will repeat on Apr, July, Oct and Jan. Code for the above is given below

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});

Please note we also need to sort the months in the ascending order. The code for the recursion is as follows

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);
Arjun Sankar
  • 190
  • 1
  • 9
0

If you want to run recurring function independently and efficiently, better use your system's scheduler i.e. Linux has crontab You can refer crontab here. Also, you can refer to crontab guru for multiple patterns. Example:

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

This will run your script.js at 09:00 in March, June, September, and December.”

mehta-rohan
  • 1,373
  • 1
  • 14
  • 19