Skip to main content
Participant
Sep 10, 2020 12:16 PM UTC
Answered

Subtracting dates to figure left over months on a form

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Sep 11, 2020 11:46 AM UTC

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

9 replies

JP_ECSAuthor
Participant
Sep 14, 2020 12:52 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Sep 14, 2020 1:00 PM UTC

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

Translate
try67
Community Expert
Community Expert
Sep 14, 2020 1:02 PM UTC

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

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Sep 11, 2020 11:46 AM UTC

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);
}
Translate
try67
Community Expert
Community Expert
Sep 10, 2020 12:49 PM UTC

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?

Translate
JP_ECSAuthor
Participant
Sep 11, 2020 11:36 AM UTC

Yes. Need to round up the month.

 

Thank you

Translate