Copy link to clipboard
Copied
In an Adobe form I am trying to calculate the number of days a vehicle is driven based on dates entered on multiple lines of the form. Vehicles may make multiple trips on one day, but only one day should be counted, not the number of trips. One trip may be made up of multiple days, so all days should be counted. This example should add up to 13 days driven. The form fields are named LeaveRow1 through LeaveRow20, and ReturnRow1 through ReturnRow20. The calculated field is DaysDriven. Can anyone provide the custom calculation script to make this work?
| Leave Date | Return Date |
| 10/1 | 10/1 |
| 10/2 | 10/2 |
| 10/3 | 10/8 |
| 10/9 | 10/9 |
| 10/9 | 10/10 |
| 10/10 | 10/13 |
Copy link to clipboard
Copied
More is needed than finding the interval between dates. There needs to be a technique for determining the inclusive dates. Then counting those days. One method would be to use an array where every element is a day in the range. A true value indicates the car was used on that day. There are probably other ways to do this.
Copy link to clipboard
Copied
Thanks, Thom, but I was hoping someone could provide the actual javascript that I could enter in the Calculations tab of the field.
Copy link to clipboard
Copied
Ok, so you are not interested in learning Acrobat JavaScript? Or doing any development yourself? How much time do you think this someone would spend developing and testing this code?
Copy link to clipboard
Copied
Perhaps I'm asking this in the wrong forum. I thought this was a place for Adobe experts to help other users, not degrade them.
Thanks for your time.
Copy link to clipboard
Copied
I would love to help you, and I started, but more information is needed for coming up with real code. And this requires your involement. You'll find many (probably hundreds) of posts on this forum for calculating a simple difference in days. But what you presented isn't simple, it requires some thought in coming up with a methodology, which I was in the process of providing. So I am the one who was degraded, not you. Rather than get involved to solve your own problem, you choose to demand a full solution from someone who is helping you for free.
Copy link to clipboard
Copied
I am working on scripts myself, have spent dozens of hours on this. My form is complete with calculations for mileage, lease rates, monthly charges, monthly charges with mileage overages, etc. This final calculation had me stumped and I simply thought someone might have run across it before. I didn't expect anyone to spend hundreds of hours working for free, and wasn't demanding anything. You never said you were working on it or that you needed more information, which I would have been happy to provide.
Copy link to clipboard
Copied
Excellent!! So you have been doing some scripting. So what's the longest it could be from Leave to Return? Could these dates cross month bounaries? Year boundaries? (because PDFAS is correct, the year should be included).
So here's the general outline.
1. Write a function that calculates the number of days between two dates.
2. Save the first and last dates.
3. Create an array that is the total number of days between the first and last dates long. Fill it with false values.
4. Walk the Leave/Return rows, For each:
a. Find the number of days from the first date to the current Leave Date. This is the index number.
b. Fill the array from the index to the number of of days between the current leave and return dates.
5. Count the number of true values in the array and that is the number of days the car was driven.
That's the solution.
Copy link to clipboard
Copied
I never said hundreds of hours. And you never proposed any type of solution. Or considered my comment about methdology. As you can see, it's quite important.
Copy link to clipboard
Copied
And one other question. Are the leave/return rows guaranteed to be consecutive?
Copy link to clipboard
Copied
Sorry for the miscommunication. As for rows being consecutive, do you mean no rows would be skipped or no dates would be skipped? There are 20 rows on the form and none would be skipped. Dates may be skipped. Vehicle could be returned on the 8th and not driven again until the 12th.
Copy link to clipboard
Copied
I'm assuming that not all rows need to be filled, and the user can fill them out in any order they want. If this is true then there could be blank rows inbetween filled rows, and the dates could be out of order in consecutive rows. Unless there is some mechanism in place that prevents the user from entering in any order they want. These assumptions control how the code will be written and the types of safety checks that will be necessary.
Copy link to clipboard
Copied
Form is done. Tested and tested. Works beautifully. Thanks for your help!
Copy link to clipboard
Copied
For date calculations your dates should include the year as well.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now