Skip to main content
Inspiring
June 23, 2023
Answered

Calculation successive dates from a start date

  • June 23, 2023
  • 1 reply
  • 675 views

I posted this as a reply to a post that I started so I'm not sure that this question will be seen by any who did not reply to the other post. Apologies if I am not following protocol.

 

In the following script the value given to "var test" assures me that the values of "var priorMo" and "var priorYr" are working as intended.

var d1 is the date of the previous payment. var d2 is the next payment due date, and will always be the first day of the following month. Both of fields d1 and d2 are date-formatted as mm/dd/yyyy. No error is thrown by this script. However, var d2 receives no value from the script. Can someone please tell me how to pass the value of the new date to var d2 as I am trying to do at the last line of the if/else statements?

var test = getField("test");
var d1 = getField("date.PaymentDue.1").value;
var d = new Date(d1);
var priorMo = d.getMonth();
var priorYr = d.getFullYear();
var d2 = getField("date.PaymentDue.2");
var d = new Date();
if(priorMo <= 10) {
test.value = "Month: " + priorMo + "\rYear: " + priorYr;
d = d.setMonth(priorMo + 1);
d = d.setDate(1);
d = d.setFullYear(priorYr);
d2.value = new Date(d);
}
else if(priorMo == 11) {
test.value = "Month: " + priorMo + "\rYear: " + priorYr;
d = d.setMonth(priorMo -11);
d = d.setDate(1);
d = d.setFullYear(priorYr + 1);
d2.value = new Date(d);
}

 

This topic has been closed for replies.
Correct answer ODuinn

This was solved with the following script:

This script accomplishes my objective employing what I marked as the correct answer, with one exception. The months are numbered from 0 through 11, so 11 is the trigger for adding another year to the next payment and changning the next month to 0. By using a loop and changing the number in the field names of the dates to "i" in the loop, you can do this for multiple payment dates.
var d1 = new Date(getField("date.PaymentDue.1").value);
var priorMo = d1.getMonth();
var priorYr = d1.getFullYear();
var d2 = getField("date.PaymentDue.i");
if(priorMo <= 10) {
d2.value = (priorMo + 2) + "\/1" + "\/" + priorYr;
}
else if(priorMo == 11) {
d2.value = ("01") + "\/1" + "\/" + (priorYr + 1);
}

1 reply

ODuinnAuthorCorrect answer
Inspiring
June 23, 2023

This was solved with the following script:

This script accomplishes my objective employing what I marked as the correct answer, with one exception. The months are numbered from 0 through 11, so 11 is the trigger for adding another year to the next payment and changning the next month to 0. By using a loop and changing the number in the field names of the dates to "i" in the loop, you can do this for multiple payment dates.
var d1 = new Date(getField("date.PaymentDue.1").value);
var priorMo = d1.getMonth();
var priorYr = d1.getFullYear();
var d2 = getField("date.PaymentDue.i");
if(priorMo <= 10) {
d2.value = (priorMo + 2) + "\/1" + "\/" + priorYr;
}
else if(priorMo == 11) {
d2.value = ("01") + "\/1" + "\/" + (priorYr + 1);
}
ODuinnAuthor
Inspiring
July 2, 2023

There is a typo in the script above. In var d2 = getField("date.PaymentDue.i") the letter "i" in the field name should be "1". Another clarification: since the months are zero-based (0 thru 11), and the value passed to var d2 is a string,  the next due date needs to add 2 to the value of var priorMo to get the correct month for the string.

Finally, it doesn't seem right to mark my reply to my own question as the correct answer, but it is good solution, so when the support community sent a few emails asking me to mark a reply as correct, I did that.