Skip to main content
Participant
October 9, 2024
Question

Custom Calculation Script for duration between two times during the same day

  • October 9, 2024
  • 3 replies
  • 1521 views

I have a fillable pdf form and it has a Start Date and a End Date with the format off HH:MM:tt . (each time is on the same date) What I need is the java script to enter into my third field of the time between the two but so far nothing I have found online has worked, even when modified. I need the results to be in hours and minutes. Can anyone help me? 

This topic has been closed for replies.

3 replies

Participant
October 10, 2024

Please note this is not related to hours worked at all. This is finding the hour or minutes between two times on the same date to see how long something occured. 

try67
Community Expert
Community Expert
October 10, 2024

The code doesn't care what this is means. It just performs the calculation you described.

Participant
October 10, 2024

Unfortunatly this is not working and in my situation I need the calculation to reflect the difference of the time down to the minute. I'm ot certain what I'm doing incorrectly. 

try67
Community Expert
Community Expert
October 10, 2024

Not working in what way, exactly? Are you getting the wrong results? No result? Are there error messages?

Can you share the file?

Participant
October 10, 2024

Attached is the file, I removed all other data due to it being confidential. I really appreciate the help. I'm very new to adobe. Thank you! 

try67
Community Expert
Community Expert
October 9, 2024

This was discussed here many times in the past, but you can use something like this:

 

var start = this.getField("Start Date").valueAsString;
var end = this.getField("End Date").valueAsString;

if (start=="" || end=="") event.value = "";
else {
	var hourInMs = 3600000;
	var startTimeDate = util.scand("mm/dd/yyyy HH:MM tt", "01/01/2024 " + start);
	var endTimeDate = util.scand("mm/dd/yyyy HH:MM tt", "01/01/2024 " + end);
	var timeDiffInHours = (endTimeDate.getTime()-startTimeDate.getTime()) / hourInMs;
	event.value = timeDiffInHours.toFixed(2);
}

 

This will result in a decimal value, so 2.5 for two and a half hours. If you want it in time notation (2:30 in the same example), replace the last line with this:

 

	var fullHoursDiff = Math.floor(timeDiffInHours);
	var minutesDiff = ((timeDiffInHours-fullHoursDiff)*60).toFixed(0);
	event.value = fullHoursDiff + ":" + (((minutesDiff.length==1) ? "0" : "") + minutesDiff);

 

Nesa Nurani
Community Expert
Community Expert
October 10, 2024

If second part is used to show result as 2:30 if it's a negative it will not calculate correctly.

try67
Community Expert
Community Expert
October 10, 2024

How can it be negative? Do you mean if the End Time is before the Start Time?