Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

62

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
community guidelines
Adobe Community Professional ,
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.

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
community guidelines
Adobe Community Professional ,
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?

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

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
community guidelines
Adobe Community Professional ,
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.

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
community guidelines
Adobe Community Professional ,
Sep 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

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

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

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
community guidelines
Adobe Community Professional ,
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;
}

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