Calculate difference between two time fields

Explorer ,
Aug 27, 2018

Copy link to clipboard

Copied

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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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 = "";

}

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

560

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Calculate difference between two time fields

Explorer ,
Aug 27, 2018

Copy link to clipboard

Copied

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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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 = "";

}

TOPICS
Acrobat SDK and JavaScript, Macintosh, Windows

Views

561

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 27, 2018 0
Most Valuable Participant ,
Aug 27, 2018

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0
Explorer ,
Aug 27, 2018

Copy link to clipboard

Copied

Hi

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0
Most Valuable Participant ,
Aug 27, 2018

Copy link to clipboard

Copied

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 = "";

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0
Explorer ,
Aug 27, 2018

Copy link to clipboard

Copied

Hi

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0
Most Valuable Participant ,
Aug 27, 2018

Copy link to clipboard

Copied

No, you can't combine the two things, unless you write your own custom keystroke and format scripts, which is not a simple task.

I don't think it's necessary, really. If the tab order is set correctly then the user just needs to press Tab after entering the value, and it will jump to the next field.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0
Explorer ,
Aug 27, 2018

Copy link to clipboard

Copied

Hi

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 27, 2018 0