Skip to main content
Participant
September 15, 2021
Question

Need to Create a Custom Calculation Script

  • September 15, 2021
  • 3 replies
  • 1722 views

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?

 

 

This topic has been closed for replies.

3 replies

try67
Community Expert
Community Expert
September 15, 2021

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;
}
try67
Community Expert
Community Expert
September 15, 2021

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

Participant
September 15, 2021

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

try67
Community Expert
Community Expert
September 15, 2021

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

ls_rbls
Community Expert
Community Expert
September 15, 2021

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.