Highlighted

## Calculate Time difference between two times

Community Beginner ,
Oct 22, 2020

Copied

I found this code from try67.  It is not working.

I have an ArrivalTime and DepartureTime they are set us as a time field, format h:MM tt (since this works best for users)  I have the TimeOnJob field formatted to None.

Should I change the format on my two time entryfields?

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;

}

else {

event.value = ""

TOPICS
Create PDFs, Edit and convert PDFs, PDF forms

Views

34

Likes

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 Time difference between two times

Community Beginner ,
Oct 22, 2020

Copied

I found this code from try67.  It is not working.

I have an ArrivalTime and DepartureTime they are set us as a time field, format h:MM tt (since this works best for users)  I have the TimeOnJob field formatted to None.

Should I change the format on my two time entryfields?

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;

}

else {

event.value = ""

TOPICS
Create PDFs, Edit and convert PDFs, PDF forms

Views

35

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 22, 2020 0
8 Replies 8
Most Valuable Participant ,
Oct 22, 2020

Copied

Might be helpful if you let us know in what way it's not working, exactly...

Likes

Report

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

Copied

Fair enough.  🙂 Nothing happens at all.  I was assuming I had my formatting wrong.  So I guess my question was how to format my input fields.

Likes

Report

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

Copied

That depends on the how the parseTime function works...

Likes

Report

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

Copied

Please post the code for the "parseTime" function.

Are there any error messages posted in the JavaScript Console window.

In fact, this code is exactly the type of thing that you should be testing and debugging in the console first, before using it in a form field.

You'll find a video tutorial here on using the Console Window:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

Likes

Report

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

Copied

I will look at the Console Window.    After 10 years of requesting Adobe Acrobat Pro at my place of employment, they finally agreed to pay the money and now I am using it for everything except what I needed it for and they expect instant results.  So I have had to rely on this community more than I would like.  Appreciate all the help.

Code I was using is below.   Format for ArrivalTime and DepartureTime is Time (h:MM tt)

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

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

}

else {

event.value = "";

}

Likes

Report

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

Copied

After figuring out to use console:

I am getting

ReferenceError: parseTime is not defined
7:Field:Mouse Up

Likes

Report

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

Copied

You must also add the function parseTime to the document.

Likes

Report

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

Copied

This is what most of the posters were getting at. There isn't a built-in function named "parseTime()" and they want to know what code was being used.

The start and stop times are acqired from the Date Object. And to get a proper date object you need more than just the time, the dates are needed as well.  If the times are known to be within the same day, then any date can be used.

In Acrobat JavaScript the "util.scand()" function is used to parse dates. Like this:

var tStart = util.scand("mm/dd/yyyy h:MM tt", cStart);

https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm

Likes

Report

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