Skip to main content
Known Participant
October 22, 2020
Question

Calculate Time difference between two times

  • October 22, 2020
  • 3 replies
  • 2985 views

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;

    event.value = totalHoursString + totalMinutesString;

}

else {

    event.value = ""

This topic has been closed for replies.

3 replies

New Participant
May 19, 2023

function convertToMinutes(time)
{
var parts = time.split(":");
var hours = parseInt(parts[0], 10);

var minutes = parseInt(parts[1], 10);

var totalMinutes = hours * 60 + minutes;

return totalMinutes;
}

var heure1 = this.getField("date3").value;
var heure2 = this.getField("date4").value;


var cminutes1 = convertToMinutes(heure1 );

var cminutes2 = convertToMinutes(heure2 );


if(heure1 != "" && heure2 != "")
{

// Calculer la différence en millisecondes
var difference = cminutes2 - cminutes1 ;


// Convertir la différence en heures et minutes
var dheures = Math.floor(difference / 60);

var dminutes = Math.floor(difference % 60);


// Afficher la différence
console.println("Différence : " + dheures + " heures et " + dminutes + " minutes");

event.value =dheures + "h:" + dminutes + " mm";


}
else {
event.value = "";
}

Bernd Alheit
Adobe Expert
May 19, 2023

Have you a issue with the function or a question?

Thom Parker
Adobe Expert
October 22, 2020

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

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
mcwadeAuthor
Known Participant
October 30, 2020

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;

event.value = totalHoursString + totalMinutesString;

}

else {

event.value = "";

}

mcwadeAuthor
Known Participant
October 30, 2020

After figuring out to use console:

 I am getting 

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

try67
Adobe Expert
October 22, 2020

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

mcwadeAuthor
Known Participant
October 22, 2020

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.

try67
Adobe Expert
October 22, 2020

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