Skip to main content
Participant
September 10, 2020
Answered

Subtracting dates to figure left over months on a form

  • September 10, 2020
  • 3 replies
  • 1323 views

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.

This topic has been closed for replies.
Correct answer try67

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);
}

3 replies

JP_ECSAuthor
Participant
September 14, 2020

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.

try67
Community Expert
Community Expert
September 14, 2020

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...

try67
Community Expert
Community Expert
September 14, 2020

Also, I don't understand which field do you want to limit... Text1 or Text2?

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
September 11, 2020

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);
}
try67
Community Expert
Community Expert
September 10, 2020

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?

JP_ECSAuthor
Participant
September 11, 2020

Yes. Need to round up the month.

 

Thank you