Copy link to clipboard
Copied
I have a fillible PDF with fields that include a start date(mm-dd-yyyy) and start time(HHMM) with corresponding end date and end time. The goal is to have the TOTAL time elapsed between the start date/time and end date/time, formated as HH:MM. This total needs to have the capacity to exceed 24 hours, as in if the start date/time is 10/01/2024 at 1000 and the end date/time is 10/02/2024 at 1100, the result can be 25. I am having no luck finding info around the web on how to allow for a total OVER 24 hours, maybe I am not using the correct search string. I have run across multiple posts with working javascripts and the one I have currently correctly calculates total time as long as it does not occur between two dates/times that result in a total larger than 24. Using the above example will result in a total time of 1:00.
The script I am using is as follows:
Copy link to clipboard
Copied
Why the modulo 24 at the end of the hours calculation? This is what's screwing up your result.
And the modulo 60 for the minutes is not needed as well.
Replace those lines with this:
// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600);
difflnMilliSeconds -= hours * 3600;
// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60);
PS. difflnMilliSeconds is badly named. It's the diff in seconds, since you're dividing the result of the difference by 1000, thereby converting it from ms to s.
Copy link to clipboard
Copied
Remove modulus operator (% 24) to allow times over 24h.
Copy link to clipboard
Copied
Why the modulo 24 at the end of the hours calculation? This is what's screwing up your result.
And the modulo 60 for the minutes is not needed as well.
Replace those lines with this:
// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600);
difflnMilliSeconds -= hours * 3600;
// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60);
PS. difflnMilliSeconds is badly named. It's the diff in seconds, since you're dividing the result of the difference by 1000, thereby converting it from ms to s.
Copy link to clipboard
Copied
Thank you! The total time now reports correctly. This is exactly the solution I needed!
Copy link to clipboard
Copied
Remove modulus operator (% 24) to allow times over 24h.
Copy link to clipboard
Copied
Thank you! I removed the value as instructed by yourself and try67 suggested and the script works exactly as I needed it to.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now