Skip to main content
Participating Frequently
October 18, 2024
Answered

Java Script - Duration between two times

  • October 18, 2024
  • 1 reply
  • 1039 views

Hello,

 

I am absolutely clueless when it comes to JavaScript, I have been down a Google rabbithole but anything i find doesn't work. I have a form i want to fill in with a start time and finish time then a column that calculates how many hours between the 2 times. I have got close but not minutes not showing correctly i.e. 8hrs 30mins is howing as 8.30 instead of 8.5. My start and end times are formatted to HH:MM. Is there also a way i can ensure the input for the start and end times is in 24hr as i don't want someont to put 12:00 - 01:00 instead of 12:00-13:00. I was thinking of doing a drop down box in 15 minute increments, but there may be a better way. Thank you!

This topic has been closed for replies.
Correct answer PDF Automation Station

It is located in Calculate > Custom calculation Script

 

 

if ((this.getField("End").value.length == 0) || (this.getField("Start").value.length == 0)) {

event.value = " 0 Hours 0 Minutes";

}

else{

 

var timefinished = this.getField("End").value;

var timestarted = this.getField("Start").

 

value; var datetimefinished = new Date('1970/01/01' + " " + timefinished);

var datetimestarted = new Date('1970/01/01' + " " + timestarted);

 

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;

 

var hours = Math.floor(difflnMilliSeconds / 3600) % 24;

difflnMilliSeconds -= hours *3600;

 

var minutes = Math.floor(difflnMilliSeconds / 60) % 60;

difflnMilliSeconds -= minutes * 60;

 

event.value = hours + ":" + minutes ;

}


Try this:

 

if ((this.getField("End").value.length == 0) || (this.getField("Start").value.length == 0)) {
event.value = " 0 Hours 0 Minutes";
}
else{
var timefinished = this.getField("End").value;
var timestarted = this.getField("Start").value;
var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);
var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds -= hours *3600;
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds -= minutes * 60;
event.value = hours + (minutes/60) ;
}

1 reply

PDF Automation Station
Community Expert
Community Expert
October 18, 2024

This has been answered many times on this forum.  Here's a recent one.  Here are a few date/time articles:

https://pdfautomationstation.substack.com/p/date-and-time-in-pdfs-part-1

https://pdfautomationstation.substack.com/p/date-and-time-in-pdfs-part-2

If 30 mins is showing as 30 instead of .5 you probably just need to add division by 60 to that result.

 

 

Participating Frequently
October 18, 2024

Thank you. However this gives me the same result i already has it is in minutes rather than decimals.

PDF Automation Station
Community Expert
Community Expert
October 18, 2024

Please provide your script and where it is located.