Skip to main content
August 4, 2016
Question

Time Calculations

  • August 4, 2016
  • 2 replies
  • 351 views

I need help calculating the duration between Time in and Time out

This topic has been closed for replies.

2 replies

August 5, 2016

Thanks - I was working off of the tutorials above, but my total keeps at 0.  I think I am missing a step

Here is my script.  The two field that I need pulled from are Time In and Time Out

var strStart = this.getField("Time InRow1").value;
var strEnd = this.getField("Time OutRow1").value;
if(strStart.length || strEnd.length) {

}

    var dateStart = util.scand("mmm d, yyyy",strStart);
    var dateEnd = util.scand("mmm d, yyyy",strEnd);

    var diff = dateEnd.getTime() - dateStart.getTime();

    // One Day = (24 hours) x (60 minutes/hour) x
    // (60 seconds/minute) x (1000 milliseconds/second)
    var oneDay = 24 * 60 * 60 * 1000;

    var days = Math.floor(diff/oneDay);

    // event.value represents the data value that will be applied to
    // the field when the calculation is finished, never explicitly
    // set a field value from within a calculation event.
    // i.e., Never do this - this.getField("fldName").value = ... event.value = days;

    var strStart = this.getField("DateStart").value;
    if(strStart.length) {
        var dateStart = util.scand("mmm d, yyyy",strStart);

            var oneDay = 24 * 60 * 60 * 1000;
            var dueMillis = dateStart.getTime() + 5 * oneDay;
            var dueDate = new Date(dueMillis);

            event.value = util.printd("mmm d, yyyy",dueDate);

        }

try67
Community Expert
Community Expert
August 5, 2016

First of all, I would check that both fields are filled in before proceeding with the calculation, not either one. So change the OR operator (||) in the first if-condition to an AND operator (&&).

Also, I would add an else statement, to reset the field in case that condition is not met.

And I don't quite understand why you have an additional declaration of the strStart variable and an additional if-statement to check whether it's empty. You don't need those.

I would also recommend that you enable the option to show the JS Console on warnings and errors (under Edit - Prefs. - JavaScript). It will help you find problems with your code as they happen.

try67
Community Expert
Community Expert
August 4, 2016

Working with time and date calculations in PDF files is a bit tricky. These tutorials are a good place to start:

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

If you don't want to mess around with writing code to do it you can use an existing script to set it up, like this one I've developed:

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