給定 3 個引數,我想知道為什么這不是迭代/生成日期,而是重復第一次迭代。
//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"));
}
}
}
第一個和第二個日期是正確的,但其他 4 個與第二個日期相同,因此沒有按預期迭代。
這是我用來添加天數的函式:
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() days);
return result;
}
謝謝!
uj5u.com熱心網友回復:
當我看到你的腳本時,firstInstallementDate是在回圈中使用的。在這種情況下,firstInstallementDate不會更新 的值。我認為這可能是您的問題的原因。如果我的理解是正確的,那么下面的修改呢?
從:
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"));
}
到:
for (let a = 0; a < condition; a ) {
firstInstallementDate = addDays(firstInstallementDate, intervalInDays);
deadlines.push(Utilities.formatDate(firstInstallementDate, Session.getTimeZone(), "dd/MM/yyyy"));
}
通過這種修改,獲得了以下結果。
[ '09/03/2022', '08/04/2022', '08/05/2022', '07/06/2022', '07/07/2022', '06/08/2022', '05/09/2022' ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439998.html
