Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
If second part is used to show result as 2:30 if it's a negative it will not calculate correctly.
Copy link to clipboard
Copied
How can it be negative? Do you mean if the End Time is before the Start Time?
Copy link to clipboard
Copied
Yes, but it's not likely to be used in this context anyway, since start time will always be before end time.
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Not working in what way, exactly? Are you getting the wrong results? No result? Are there error messages?
Can you share the file?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I changed that and it gives me 0.07 as the calculation from 1.:55 am to 1:59 am?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
The code doesn't care what this is means. It just performs the calculation you described.
Copy link to clipboard
Copied
Are your fields named "Start Date" and "End Date"?
Where did you place the script?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more