Given 3 parameters, I'd like to know why this is not iterating/generating the dates, but repeating the first iteration.
//Means 01 installement for tomorrow and the other 6 from tomorrow on every 30 days
function generateInstallments() {
let condition = '1 6x'
let firstInstallment = 1;//Tomorrow
let intervalInDays = 30;
var dueDate = '';
let deadlines = [];
if (condition.indexOf(' ') > -1) { //If it contains this pattern (1 4x)
//Extracts the number preceding x, as this will be the number of installments
condition= Number(condition.slice(
condition.indexOf(' ') 2,
condition.lastIndexOf('x'),
));
let firstInstallementDate = addDays(new Date(), firstInstallment );
deadlines.push(Utilities.formatDate(firstInstallementDate , Session.getTimeZone(), "dd/MM/yyyy"));
for (let a = 0; a < condition; a ) {
dueDate = addDays(firstInstallementDate , intervalInDays );//Function can be found below
deadlines.push(Utilities.formatDate(dueDate, Session.getTimeZone(), "dd/MM/yyyy"));
}
}
}
The first and 2 second dates are correct, but the other 4 are the same as the second one, so this is not iterating as expected.
This is the function I'm using to add the days:
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() days);
return result;
}
Thank you!
CodePudding user response:
When I saw your script, firstInstallementDate
is used in the loop. In this case, the value of firstInstallementDate
is not updated. I thought that this might be the reason for your issue. If my understanding is correct, how about the following modification?
From:
for (let a = 0; a < condition; a ) {
dueDate = addDays(firstInstallementDate , intervalInDays );//Function can be found below
deadlines.push(Utilities.formatDate(dueDate, Session.getTimeZone(), "dd/MM/yyyy"));
}
To:
for (let a = 0; a < condition; a ) {
firstInstallementDate = addDays(firstInstallementDate, intervalInDays);
deadlines.push(Utilities.formatDate(firstInstallementDate, Session.getTimeZone(), "dd/MM/yyyy"));
}
By this modification, the following result is obtained.
[ '09/03/2022', '08/04/2022', '08/05/2022', '07/06/2022', '07/07/2022', '06/08/2022', '05/09/2022' ]