Highlighted

Subtracting dates to figure left over months on a form

New Here ,
Sep 10, 2020

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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);
}
TOPICS
Acrobat SDK and JavaScript, Comment review and collaborate PDF, Create PDFs, PDF forms

Views

69

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Subtracting dates to figure left over months on a form

New Here ,
Sep 10, 2020

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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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);
}
TOPICS
Acrobat SDK and JavaScript, Comment review and collaborate PDF, Create PDFs, PDF forms

Views

70

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 10, 2020 0
Most Valuable Participant ,
Sep 10, 2020

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 10, 2020 0
New Here ,
Sep 11, 2020

Copy link to clipboard

Copied

Yes. Need to round up the month.

 

Thank you

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Most Valuable Participant ,
Sep 11, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
New Here ,
Sep 14, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0
New Here ,
Sep 14, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

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 = "";

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 14, 2020 0
JP_ECS LATEST
New Here ,
Sep 15, 2020

Copy link to clipboard

Copied

Thank you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 15, 2020 0