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

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

New Here ,
Oct 09, 2024 Oct 09, 2024

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? 

TOPICS
JavaScript
1.6K
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 09, 2024 Oct 09, 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);

 

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 09, 2024 Oct 09, 2024

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

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 10, 2024 Oct 10, 2024

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

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 10, 2024 Oct 10, 2024

Yes, but it's not likely to be used in this context anyway, since start time will always be before end time.

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 10, 2024 Oct 10, 2024

Yes, that was my assumption. It's good to add a validation of the user's input, though, that's true...

Something like this should do the trick:

 

if (timeDiffInHours<0) event.value = "ERROR: Start time must be before End Time";

else {

// put rest of code here

}

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 10, 2024 Oct 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. 

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 10, 2024 Oct 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?

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 10, 2024 Oct 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! 

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 10, 2024 Oct 10, 2024

The field names in this file are not what you said they were. They are "Start Time" and "End Time", not "Start Date" and "End Date". Adjust the code accordingly and try again.

 

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 10, 2024 Oct 10, 2024

I changed that and it gives me 0.07 as the calculation from 1.:55 am to 1:59 am? 

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 10, 2024 Oct 10, 2024
LATEST

That's correct. 4/60 = 0.066666 rounded to 0.07.

If you want a different notation I explained how to do it in my original reply.

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 10, 2024 Oct 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. 

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 10, 2024 Oct 10, 2024

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

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 10, 2024 Oct 10, 2024

Are your fields named "Start Date" and "End Date"?

Where did you place the script?

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