Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every 3 and 6 Months Recurrence on later.js stating from a particular date

Tags:

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);
like image 929
Aravind Srinivas Avatar asked Jun 06 '18 12:06

Aravind Srinivas


1 Answers

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);
like image 75
Arjun Sankar Avatar answered Sep 28 '22 17:09

Arjun Sankar