JavaScript Form Field Time Calculation
Copy link to clipboard
Copied
I am trying to calculate the difference between 2 form time fields in a 3rd field. I have set the formats for all 3 to none and the time fields are in the format 12:00. The script I entered for the 3rd result field is:
_____________________________________________________________________
var cStart = this.getField("IN").value;
var cStop = this.getField("OUT").value;
if (cStart != "" && cStop != "") {
var tStart = parseTime(cStart);
var tStop = parseTime(cStop);
var total = (tStop - tStart)/(1000*60*60);
var totalHours = Math.floor(total);
var totalMinutes = (total-totalHours)*60;
var totalHoursString = (totalHours<10) ? "0"+totalHours : ""+totalHours;
var totalMinutesString = (totalMinutes<10) ? "0"+totalMinutes : ""+totalMinutes;
event.value = totalHoursString + totalMinutesString;
}
else {
event.value = "";
______________________________________________________________
I am not getting any result and the field remains blank. I would appreciate any suggeastions.
Thanks!
Copy link to clipboard
Copied
There is no function called parseTime() in Acrobat's JavaScript. You should have gotten an error message on the JavaScript console. If you need to distinguish between AM and PM, it gets a bit more involved (for 24h format, you can just split on the ":" and process the two elements into minutes since midnight). I would create a fake data/time string, using e.e. 1/1/2023 as the date and the actual time. Then you can use the util.scand() function to parse that string and convert to a date object.
Copy link to clipboard
Copied
This question has already been answered dozens of times.
Please start here:
PDF Acrobatic, InDesigner & Photoshoptographer

