• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Subtracting dates to figure left over months on a form

New Here ,
Sep 10, 2020 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.

TOPICS
Acrobat SDK and JavaScript

Views

537

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
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 11, 2020 Sep 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);
}

Votes

Translate

Translate
Community Expert ,
Sep 10, 2020 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?

Votes

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
community guidelines
New Here ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

Yes. Need to round up the month.

 

Thank you

Votes

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
community guidelines
Community Expert ,
Sep 11, 2020 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);
}

Votes

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
community guidelines
New Here ,
Sep 14, 2020 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.

Votes

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
community guidelines
Community Expert ,
Sep 14, 2020 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...

Votes

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
community guidelines
Community Expert ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
New Here ,
Sep 14, 2020 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.

Votes

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
community guidelines
Community Expert ,
Sep 14, 2020 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 = "";

 

Votes

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
community guidelines
New Here ,
Sep 15, 2020 Sep 15, 2020

Copy link to clipboard

Copied

LATEST

Thank you!

Votes

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
community guidelines