Skip to main content
July 12, 2019
Question

Time Elapsed Calculation HH:MM:ss

  • July 12, 2019
  • 2 replies
  • 1132 views

Hello Experts,

I'm a bit of a java rookie but usually I can get a code to swing out the way I need it to do. 

I have spent a week working through forums searching for a solution but as yet have not found a code that is effective.
I have a PDF which needs to calculate the time difference between two sets of time input.

Time1 = HH:MM:ss

and
Time3 = HH:MM:ss

OR (ideally)

Time1 = DD/MM/YYYY HH:MM:ssand

Time3 = DD/MM/YYYY HH:MM:ss

I have managed to make the date calculation work stand alone but ideally I would have it occur in the one calculation.

The code is reactive and only occurs with a specific drop down result.

Below is the only time calculation I have managed to get to work. It displays accurately in decimals, but does not calculate seconds yet, and try as I might when I modify the code to attempt the required format I can't get it to work. 

I am using Adobe 2017

//get whatif value

var cDisplay = this.getField("Dropdown8").value

// compute number of days only if there are values for both dates and displays

if(cDisplay != "A" & cDisplay != "C" & cDisplay != "D" & cEnd != "" & cStart != '')

//

var cDisplay = this.getField("Dropdown8").value

var strStart = this.getField("Time1").value;

var strEnd = this.getField("Time3").value;

strStart = strStart + " 1/1/70";

strEnd = strEnd + " 1/1/70";

if(cDisplay != "A" & cDisplay != "C" & cDisplay != "D" & cEnd != "" & cStart != '')

{

   var timeStart = util.scand("hh:MM ", strStart);

   var timeEnd = util.scand("hh:MM ", strEnd);

   if (timeEnd < timeStart) {

   var strEnd = this.getField("Time3").value

   var strEnd = strEnd + " 1/2/70";

   var timeEnd = util.scand("hh:MM", strEnd);

   var diff = timeEnd - timeStart;

   var oneHour = 60 * 60 * 1000;

   var timeleft = (diff/oneHour);

}

var diff = timeEnd - timeStart;

var oneHour = 60 * 60 * 1000;

var timeleft = (diff/oneHour);

//event displayed

event.value = timeleft.toFixed(2);

} else {

event.value = " ";

}

This topic has been closed for replies.

2 replies

July 23, 2019

Thanks to the efforts of try67, I used the following code

var strStart = this.getField("Time1").value; 

var strEnd = this.getField("Time3").value; 

if (strStart=="" || strEnd=="") event.value = ""; 

else { 

  var timeStart = util.scand("dd/mm/yyyy hh:MM:ss", strStart); 

  var timeEnd = util.scand("dd/mm/yyyy hh:MM:ss",  strEnd); 

    if (timeEnd < timeStart) { 

        timeEnd = util.scand("dd/mm/yyyy hh:MM:ss", strEnd); 

    } 

    var diffInMs = timeEnd - timeStart; 

    var oneSecondInMS = 1000; 

    var oneMinuteInMs = 60 * oneSecondInMS; 

    var oneHourInMs = 60 * oneMinuteInMs; 

    var diffInHours = Math.floor(diffInMs/oneHourInMs); 

    diffInMs = diffInMs-(diffInHours*oneHourInMs); 

    var diffInMinutes = Math.floor(diffInMs/oneMinuteInMs); 

    diffInMs = diffInMs-(diffInMinutes*oneMinuteInMs); 

    var diffInSeconds = Math.floor(diffInMs/oneSecondInMS); 

    

    event.value = util.printf("%02d",diffInHours) + ":" + util.printf("%02d",diffInMinutes) + ":" + util.printf("%02d",diffInSeconds); 


Input is dd/mm/yyyy hh:mm:ss
The result is hh:mm:ss

Superb!

July 12, 2019

I know the code I have quoted does not reflect the needs I have stipulated, I just posted it to show what I have managed to get to work, even if the results are not what I need yet.

try67
Community Expert
Community Expert
July 12, 2019

The code above is a bit problematic and contains all kind of irrelevant stuff, but before trying to fix it you need to describe what the output should look like. You say you to include seconds in it, but in what format? Let's say the difference between the two times is 1 hour, 23 minutes and 45 seconds. What should be the value of the target field?

July 15, 2019

Hello Try67,

I know my code sucks a bit but that's why I am here.

Sorry for not being clear enough.

The input needs to be (dd/mm/yyyy hh:mm:ss) minus (dd/mm/yyyy hh:mm:ss)  and the output needs to be the same dd/mm/yyyy hh:mm:ss