Skip to main content
Known Participant
July 18, 2023
Question

Problems with calculating time differences in Form fields

  • July 18, 2023
  • 2 replies
  • 1891 views

Form fields are:

Date -(generated by a button action. code= this.getField("Date").value = util.printd("mmm d, yyyy",new Date()); 

Start -Time Formatted to HH:MM

Finish-Time Formatted to HH:MM

Result -just require a decimal hrs value as a number is fine

I want the result field to display the decimal hours difference between the start and finish time and this difference may cross midnight, but not more than 24hrs difference total. There is a lot of info on this, For example this works

Credit: Thom Parker

var strStart = this.getField("Start").value;

var strEnd = this.getField("Finish").value;

if(strStart.length && strEnd.length)

{

  var dateStart = util.scand("dd/mm/yy HH:MM",strStart);

  var dateEnd = util.scand("dd/mm/yy HH:MM,",strEnd);

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

  var onehour = 60 * 60 * 1000;

var oneDay = 24 * 60 * 60 * 1000 

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

  event.value = (diff/onehour);

}

else

  event.value = "";

 

BUT I cannot get any of it to work without having to make the time fields formats into dd/mm/yy HH:MM.

I would like to be able to keep the time entries as simple as possible.

It would be a bonus if the Time fields did not require the : to be typed each time as there are a few of them to complete. ie typing the keystrokes 0800 would automatically fill the field as 08:00.

Would hidden fields adding date in millisec to time in millisec help?

Then use variables to subtract these field values and if the result is negative add 24hrs worth of millisec

Then convert to an number of hrs. The result is best as a number value anyway for further calculation

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
July 18, 2023

The code is not complete and has errors. First, as pointed out by Try67, a date needs to be added to the time for it to parse.

But the midnight crossing must also be taken into consideration. As written, the times are expected to be within the same day. 

Here's the fixed code:

var strStart = this.getField("Start").value;
var strEnd = this.getField("Finish").value;
if(strStart.length && strEnd.length)
{
  var dateStart = util.scand("dd/mm/yy HH:MM","01/01/23 " + strStart);
  var dateEnd = util.scand("dd/mm/yy HH:MM,","01/01/23 " + strEnd);
  var diff = dateEnd.getTime() - dateStart.getTime();
  var onehour = 60 * 60 * 1000;
  var hours = diff/onehour;
  // correct for midnight crossing
  if(diff < 0)
     hours += 24;
  event.value = util.printf("%.2f",hours);
}
else
  event.value = "";

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
July 19, 2023

Thank you and Try67 for your help.

That very nearly works but it will not accept the final statement - else event.value = ''''; - it returns a syntax error message for this linenumber

I have it calculating correctly with the following change: (deleteing the last line)

var strStart = this.getField("Start").value;

var strEnd = this.getField("Finish").value;

if(strStart.length && strEnd.length);

{

var dateStart = util.scand("dd/mm/yy HH.MM","01/01/23 " + strStart);

var dateEnd = util.scand("dd/mm/yy HH.MM,","01/01/23 " + strEnd);

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

var onehour = 60 * 60 * 1000;

var hours = diff/onehour;

// correct for midnight crossing

if(diff < 0)hours += 24;

event.value = util.printf("%.2f",hours);

}

 

However this leaves an incorrect value if either of the Start or Finish  fields are blank. (I understand this removed statement is what is supposed to generate the blank Result field) but I cannot get it to accept the code with the statement there.

What is needed is for the Result field to be blank if either of the Start or Finish or both fields are blank. (Sometimes they need to be  for  particular individual/s)

 

 

Nesa Nurani
Community Expert
Community Expert
July 19, 2023

You have wrong quotes in 'else' statement, use this:

else event.value = "";

 

Also remove semicolon in this line:

if(strStart.length && strEnd.length);

try67
Community Expert
Community Expert
July 18, 2023

You can fill in the part of the code that specifies the date to some arbitrary value, without any user input.
For example, let's say the user enters "0800" as the start time and "2100" as the end time.

You can adjust your code to something like this:

 

var dateStart = util.scand("dd/mm/yy HHMM", "01/01/23 " + strStart);

var dateEnd = util.scand("dd/mm/yy HHMM,","01/01/23 " + strEnd);