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

Need to Create a Custom Calculation Script

New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

OK so I created a form and I'm stuck in the calculations. I have a start date, an end date and a dollar amount. I need to calculate a dollar amount based on the number of days.

 

Example:

Start Date  is 8/1/21

End Date is 8/4/21

Dollar Amount is $200

So the easy way to explain is 8/1/21 through 8/4/21 is 3 days... 3 days * $200 is $600

 

The dates and dollar amount will be entered by the employee and the result of $600 will automatically be calculted.

 

How can I accomplish this?

 

 

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

1.0K

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, 2021 Sep 14, 2021

Copy link to clipboard

Copied

You can accomplish this by creating your own script with Acrobat JavaScript. Have you tried doing your own script?

 

If so, would you mind sharing it?

 

In the meanwhile, see this tutorial from ACP Thom Parker on how to calculate the total number of days based on two dates: 

 

 

Check the sections that explain how to calculatethe difference in  total number of days. Then add to your script a conditional statement to calculate the total number of days obtained from the two dates and multiply by the desired dollar anount or by obtaining the value from another field object in your PDF form.

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 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

Why is the result 3, in this case? Does it not include the first day? What should happen if both dates are the same?

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, 2021 Sep 15, 2021

Copy link to clipboard

Copied

The dates will never be the same in this specific document.

 

8/1 - 8/2 is one day

8/1 - 8/3 is two days

8/1 - 8/4 is three days

etc etc etc

 

i need the formula result to calculate the number of days between the dates * $200 per day

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 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

OK. However, without the year this is not going to work properly, unless you make assumptions about the length of the period.

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 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

Sorry, I see you did include the year in your original post...

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, 2021 Sep 15, 2021

Copy link to clipboard

Copied

That was just a quick example...the form has the year. They select from a calendar 

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 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

LATEST

You can use this code as the custom calcualtion script of the amount field:

 

var startDateString = this.getField("Start Date").valueAsString;
var endDateString = this.getField("End Date").valueAsString;
if (startDateString=="" || endDateString=="") event.value = "";
else {
	var d1 = util.scand("m/d/yy", startDateString);
	var d2 = util.scand("m/d/yy", endDateString);
	var diffInDays = Math.round((d2.getTime()-d1.getTime())/86400000);
	event.value = diffInDays*200;
}

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