Copy link to clipboard
Copied
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 = " ";
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I've removed some of the irrelevant parts of the code and tried to simplify it and narrow it down to the bare basics.
Give this version a try:
var strStart = this.getField("Time1").value;
var strEnd = this.getField("Time3").value;
if (strStart=="" || strEnd=="") event.value = "";
else {
var timeStart = util.scand("mm/dd/yyyy hh:MM:ss", "01/01/1970 " + strStart);
var timeEnd = util.scand("mm/dd/yyyy hh:MM:ss", "01/01/1970 " + strEnd);
if (timeEnd < timeStart) {
timeEnd = util.scand("mm/dd/yyyy hh:MM:ss", "01/02/1970 " + 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);
}
If that works you can add the rest of the logic to it.
Copy link to clipboard
Copied
Hello try67,
I spent some time attempting to use this code but it will not display beyond " 00:00:00" in the answer box.
Maybe the answer box needs to be "dd HH:MM:ss"
I'm going to keep at it.
Really appreciate your help try67
Copy link to clipboard
Copied
It was working fine for me... Can you share your file with us (either via a file-sharing website like Dropbox, Google Drive, Adobe DC, etc., or privately via email)?
Copy link to clipboard
Copied
Hello try67,
Sorry for the delayed reply. Severe illness kept me away from my usual occupation.
I have sent you a message with a google link to the file.
Cheers,
Copy link to clipboard
Copied
Got it and replied via PM. Hope you're feeling better!
Copy link to clipboard
Copied
If you are in an area that is covered by Day Light Savings time and your time interval may include the date and time change time, your code will not work.
Copy link to clipboard
Copied
Thankfully we do not have Daylight savings so that is not an issue. Thanks for making me aware of it in the future however.
Copy link to clipboard
Copied
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!