Copy link to clipboard
Copied
Have a form that I use for signing up contractors for a yearly program. While the yearly signup begins 3/31/xxxx, I may sign them up during the year and it is a prorated fee based off the difference of what date they signup before that 3/31/xxxx date. Also we deduct one extra month from the time period and that is their fee.
Example:
3/31/2021 - 9/10/2020 = 6 months - 1 month = 5 months
Both dates have a place on the form along with the final 5 months which shows as a number.
Looking for the code to do the math on my form. Tried some Java Code i know and it isn't working out.
Thank you for looking at this and any help you can contribute.
You can use this code as the custom calculation script of the months field. Adjust the names of the fields as needed in the first two lines, of course:
var s1 = this.getField("Text1").valueAsString;
var s2 = this.getField("Text2").valueAsString;
if (s1=="" || s2=="") event.value = "";
else {
var d1 = util.scand("m/dd/yyyy", s1);
var d2 = util.scand("m/dd/yyyy", s2);
var diffInMonths = (d2.getMonth()-d1.getMonth());
event.value = (diffInMonths-1);
}
Copy link to clipboard
Copied
So you're only interested in the month value? It doesn't matter if the date is the first day of the month, or the last?
Copy link to clipboard
Copied
Yes. Need to round up the month.
Thank you
Copy link to clipboard
Copied
You can use this code as the custom calculation script of the months field. Adjust the names of the fields as needed in the first two lines, of course:
var s1 = this.getField("Text1").valueAsString;
var s2 = this.getField("Text2").valueAsString;
if (s1=="" || s2=="") event.value = "";
else {
var d1 = util.scand("m/dd/yyyy", s1);
var d2 = util.scand("m/dd/yyyy", s2);
var diffInMonths = (d2.getMonth()-d1.getMonth());
event.value = (diffInMonths-1);
}
Copy link to clipboard
Copied
Works great. text 1 (3/31/2021) and text2 (9/14/2020) rounds up to October and the count is right. Is there anyway to limit the entry of text 2 so the user can't enter a date past or before text 1? In this case text 1 can only be (04/01/2020 thru 3/30/2021)?
Thank you for your help.
Copy link to clipboard
Copied
Yes, it's possible to do that using a validation script, but then you would need to also create one for Text1, or have Text1 reset Text2 when the value of the former is entered...
Copy link to clipboard
Copied
Also, I don't understand which field do you want to limit... Text1 or Text2?
Copy link to clipboard
Copied
text 1 is always the further one out. The yearly date. text 2 is always the date before that but never more than a year before.
Copy link to clipboard
Copied
You can use something like this as the custom validation script of Text2:
var s1 = this.getField("Text1").valueAsString;
var s2 = event.value;
if (s1=="" && s2!="") {
event.rc = false;
app.alert("You must first fill in Text1");
} else if (s1!="" && s2!="") {
var d1 = util.scand("m/dd/yyyy", s1);
var d2 = util.scand("m/dd/yyyy", s2);
if (d1.getTime()<d2.getTime()) {
event.rc = false;
app.alert("You must enter a date after Text1.");
}
}
I would add this as the validation of Text1:
if (event.value) this.getField("Text2").value = "";
Copy link to clipboard
Copied
Thank you!