Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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) ;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you. However this gives me the same result i already has it is in minutes rather than decimals.
Copy link to clipboard
Copied
Please provide your script and where it is located.
Copy link to clipboard
Copied
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 ;
}
Copy link to clipboard
Copied
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) ;
}
Copy link to clipboard
Copied
That worked, thank you so much for your quick help 🙂
Find more inspiration, events, and resources on the new Adobe Community
Explore Now