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

Need help calculating time in HH:MM using mm-dd-yyyy and HHMM(military) that can exceed 24 hours

New Here ,
Oct 16, 2024 Oct 16, 2024

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.

hundin_0313_0-1729075715403.png

 

The script I am using is as follows:

 

var timefinished = this.getField("outage_end_time").valueAsString;
var timestarted = this.getField("outage_start_time").valueAsString;
 
var datefinished = this.getField("outage_end_date").valueAsString;
var datestarted = this.getField("outage_start_date").valueAsString;
 
if (datefinished && datestarted && timefinished && timestarted) {
var datetimefinished = util.scand("mm-dd-yyyy HH:MM", datefinished + " " + timefinished);
var datetimestarted = util.scand("mm-dd-yyyy HH:MM", datestarted + " " + timestarted);
 
var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;
 
// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds -= hours *3600;
 
// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds -= minutes * 60;
 
// set field value to the difference
event.value = hours + ":" + minutes;
} else event.value = "";
 
The addition of milliseconds is not important, but the resulting total so far is acceptable, outside of not being reported as MORE than 24. My question is this: can the total time result in a true hour calculation, or am I asking for the moon? Do I need to either add a string to the code, or is the calculation not appropriate for the use. I am certainly not well versed with javascript, any help/advice/suggestions would be greatly appreciated.
TOPICS
General troubleshooting , How to , JavaScript , Modern Acrobat , PDF , PDF forms
586
Translate
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Oct 16, 2024 Oct 16, 2024

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.

View solution in original post

Translate
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 ,
Oct 16, 2024 Oct 16, 2024

Remove modulus operator (% 24) to allow times over 24h.

View solution in original post

Translate
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 ,
Oct 16, 2024 Oct 16, 2024

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.

Translate
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 ,
Oct 16, 2024 Oct 16, 2024

Thank you! The total time now reports correctly. This is exactly the solution I needed!

Translate
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 ,
Oct 16, 2024 Oct 16, 2024

Remove modulus operator (% 24) to allow times over 24h.

Translate
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 ,
Oct 16, 2024 Oct 16, 2024
LATEST

Thank you! I removed the value as instructed by yourself and try67 suggested and the script works exactly as I needed it to.

Translate
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