Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
How can I generate the deadlines given 03 parameters in Google Apps Script?

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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'
      ]
    
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!