Skip to main content
Participating Frequently
Oct 18, 2024 1:06 PM UTC
Answered

Java Script - Duration between two times

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 is closed to new replies. Start a new post to keep the conversation going.
PDF Automation Station
Community Expert
Oct 18, 2024 2:34 PM UTC

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

6 replies

PDF Automation Station
Community Expert
Community Expert
Oct 18, 2024 1:17 PM UTC

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.

 

 

Translate
Participating Frequently
Oct 18, 2024 1:23 PM UTC

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

Translate
PDF Automation Station
Community Expert
Community Expert
Oct 18, 2024 1:38 PM UTC

Please provide your script and where it is located.

Translate