Skip to main content
clavin007
Inspiring
August 27, 2018
Answered

Calculate difference between two time fields

  • August 27, 2018
  • 1 reply
  • 4921 views

Hi all,

I would appreciate it so much if you could help me out a little with this.

I have a starttime and endtime fields but I would like the form to auto calculate for me once these two fields have been populated.

Example:

  1. How do I make the form calculate “Duration” for me and the “Total” as well.
  2. How do I make the form let the user to move from column to column automatically? For example, after entering start time (HHMM), the cursor would automatically move to the End time column?

Thank you and Regards,

James

This topic has been closed for replies.
Correct answer try67

Change the field's Format to None and use this code for the calculation (this is for the A row):

var cStart = this.getField("Start1").value;

var cStop = this.getField("Stop1").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 = "";

}

1 reply

try67
Community Expert
Community Expert
August 27, 2018

Are the times always in the same calendar day?

This issue was discussed many times in the past, and complete code examples have been given to answer it.

You should really try searching the forums for "time calculation", or something to that extent.

You might also find these tutorials useful:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-3

And if you want a (paid-for) tool that will set it up for you with a few clicks, check out this one I've developed:

Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet

Regarding your second question: Do you mean that after the user entered the four digits it will automatically jump to the next field?

clavin007
clavin007Author
Inspiring
August 27, 2018

Hi

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 27, 2018

Change the field's Format to None and use this code for the calculation (this is for the A row):

var cStart = this.getField("Start1").value;

var cStop = this.getField("Stop1").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 = "";

}