Copy link to clipboard
Copied
Hello, I don't do javascript here and while I tried searching.....I am not sure if any of the results I landed on pertain to me. Here are the fields I have.
Start Time
End Time
Total Time (In minutes)
The start/end times are military time format. Just need the Total Time filed calculated and displayed in minutes.
Copy link to clipboard
Copied
Assuming the values in Start Time and End Time follow this format: 15:31, enter this custom calculation script in the Total Time field:
var startHour=this.getField("Start Time").value.split(":")[0];
var startMin=this.getField("Start Time").value.split(":")[1];
var endHour=this.getField("End Time").value.split(":")[0];
var endMin=this.getField("End Time").value.split(":")[1];
event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);
Copy link to clipboard
Copied
Please post the exact format of Start Time and End Time.
Copy link to clipboard
Copied
Assuming the values in Start Time and End Time follow this format: 15:31, enter this custom calculation script in the Total Time field:
var startHour=this.getField("Start Time").value.split(":")[0];
var startMin=this.getField("Start Time").value.split(":")[1];
var endHour=this.getField("End Time").value.split(":")[0];
var endMin=this.getField("End Time").value.split(":")[1];
event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);
Copy link to clipboard
Copied
This also assumes the end time is on the same date as the start time. If the start time is 23:50 and the end time is 00:25, it won't work correctly.
Copy link to clipboard
Copied
how would you do it if start time is 23:50 and end time is 00:25?
Copy link to clipboard
Copied
HH:MM. And I don't know if it matters, but any hours entered would be during the day time. So there should never be a situation where someone is putting in times around midnight.
Copy link to clipboard
Copied
My script should work then.
Copy link to clipboard
Copied
Yes it does! Thank you. How do I resolve the "The value entered does not match the format of the field" error? I searched and found this to try in the custom validation script section. But it doens't seem to work. I just want all the fields to display as zero until it needs to do calcuations.
if(event.value == 0)event.value = "";
I need something to the effect of...if the total fields are 0, then null them out?
Copy link to clipboard
Copied
Set the field's Format setting to None.
Copy link to clipboard
Copied
I did try that right after my last post. But now it is showing "NaN" in the total minutes field.
Copy link to clipboard
Copied
> But now it is showing "NaN" in the total minutes field.
That means the values of the fields are not what you expect them to be.
For example, when one of the fields is empty the code will not work properly.
Try this:
var startTimeString = this.getField("Start Time").valueAsString;
var endTimeString = this.getField("End Time").valueAsString;
if (startTimeString=="" || endTimeString=="") event.value = "";
else {
var startHour=startTimeString.split(":")[0];
var startMin=startTimeString.split(":")[1];
var endHour=endTimeString.split(":")[0];
var endMin=endTimeString.split(":")[1];
event.value=Number(60*(endHour-startHour)) + Number(endMin-startMin);
}
Copy link to clipboard
Copied
Correct I should have clarified that it only shows when fields are empty. I have 10 rows of start/end time fields. In probably most situations not all of them will be used. But obviously I don't want NaN to show so the form will look cleaner.
Copy link to clipboard
Copied
When you split a string by a separator (":") into an array and that separator does not exist, then you call an index greater than 0 ("[1]"), it will return "undefined". When you force "undefined" into a number ("Number(...)") it will return "NaN" (not a number). From an end user perspective, you should either validate the start and end time fields to the required format, or go with separate dropdowns for hours and minutes. try67 's script will remove the NaN's when the field is blank.
Copy link to clipboard
Copied
This should not happen with the new code I provided.
Copy link to clipboard
Copied
Alright, I got in these each of the fields and adjusted the names. Then the calucations were not updating and I finally found where the dang calculation order menu was hidden! I think I got it so far. There's one last piece to this form, but I will make a separate post on it. Thank you so much.
Copy link to clipboard
Copied
How would you then convert minutes to decimal hours?
Copy link to clipboard
Copied
Divide minutes by 60.
Copy link to clipboard
Copied
Duh. How do I add the code? This doesn't work:
Copy link to clipboard
Copied
To clarify, divide the TOTAL minutes by 60. Move /60 outside the last bracket.
Copy link to clipboard
Copied
Yay!! I'm sure there's code to do it, but I just formatted the cell to number with two decimal points.
Thank You!
Copy link to clipboard
Copied
Yes. The code would be (add this at the end):
event.value=Math.round(event.value*100)/100;
Copy link to clipboard
Copied
Try this:
var startTimeString = this.getField("TimeIn1").valueAsString;
var endTimeString = this.getField("TimeOut1").valueAsString;
if (startTimeString && endTimeString) {
var [startHour, startMin] = startTimeString.split(":").map(Number);
var [endHour, endMin] = endTimeString.split(":").map(Number);
var diffMinutes = (endHour * 60 + endMin) - (startHour * 60 + startMin);
event.value = diffMinutes / 60;}
else
event.value = "";
Copy link to clipboard
Copied
This might work better from an end user stand point if you have two dropdown fields for each time: One for Hours (0-23) and another for minutes (0-59), with a colon between them.
Copy link to clipboard
Copied
But would this affect calculations?
Copy link to clipboard
Copied
Instead of splitting the string by the : you would get the numbers from the start hours, end hours, start minutes, and end minutes fields.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more