Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculate Time difference between two times

Explorer ,
Oct 22, 2020 Oct 22, 2020

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

TOPICS
Create PDFs , Edit and convert PDFs , PDF forms
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2020 Oct 22, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 22, 2020 Oct 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.

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

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2020 Oct 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 30, 2020 Oct 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 = "";

}

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

After figuring out to use console:

 I am getting 

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

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

You must also add the function parseTime to the document. 

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

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);

 

You can read more about date and time handling here:

https://acrobatusers.com/tutorials/date_time_part1/  

https://acrobatusers.com/tutorials/date_time_part2/index.html 

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

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 19, 2023 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 = "";
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 19, 2023 May 19, 2023
LATEST

Have you a issue with the function or a question?

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