Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Java Script - Duration between two times

New Here ,
Oct 18, 2024 Oct 18, 2024

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!

TOPICS
How to , JavaScript , PDF forms
670
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Oct 18, 2024 Oct 18, 2024

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2024 Oct 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.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 18, 2024 Oct 18, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2024 Oct 18, 2024

Please provide your script and where it is located.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 18, 2024 Oct 18, 2024

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 ;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 18, 2024 Oct 18, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 18, 2024 Oct 18, 2024
LATEST

That worked, thank you so much for your quick help 🙂 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines