Copy link to clipboard
Copied
// Time Values var cStartTime = this.getField("TimeIn.0").value; var cEndTime = this.getField("TimeOut.0 ").value; // Only process if field contains a value if ((cStartTime != "") && (cEndTime != "")) { // Convert to Hours Decimal value var nStartTime = 0, nEndTime = 0; var aStartTime = cStartTime.split(":"); nStartTime = Number(aStartTime[0]) + Number(aStartTime[1]) / 60; var aEndTime = cEndTime.split(":"); nEndTime = Number(aEndTime[0]) + Number(aEndTime[1]) / 60; // Find Difference var nTimeDiff = nEndTime - nStartTime; // Test for Midnight Crossover if (nTimeDiff < 0) { // Shift 24 hours nTimeDiff += 24; } // If used in a calculation, may need to be changed to "event.value =" var nHours = Math.floor(nTimeDiff); var nMinutes = Math.floor((nTimeDiff - nHours) * 60 + 0.5); event.value = util.printf("%02d:%02d", nHours, nMinutes); } else event.value = "";
Copy link to clipboard
Copied
Your jumble of code does not work. You need to include line breaks in your code or at least after the comments so the whole mess is not a comment.
I would compute the interval as the number of minutes and then use the "Custom Format Script" to format the displayed format.
If you need to total the total time for a column of time intervals with your provided code, one would need to convert the time strings back into a numeric value before summing.
It looks like your code will not compute correctly if the time interval includes the time at which Daylight Savings Time or Summer Time changes in the Spring and in the Fall.
Copy link to clipboard
Copied
So this is the code I have but I want to display in decimal hours.
event.value = '';
var fDiff = 0;
/*
// compute the difference for first pair of fields
// get the start time
var sStart = this.getField('Field5').value;
// get the end time
var sEnd = this.getField('Field6').value;
// complete script only if we have data if(sStart != '' & sEnd != '') {
// convert sStart string to seconds
var fStart = Time2Num('hh:mm', sStart);
// convert sEnd string to seconds var fEnd = Time2Num('hh:mm', sEnd);
// compute difference in seconds fDiff += fEnd - fStart;
}
*/
fDiff = TimeDiff('Field5', 'Field6');
// compute and add the difference for the second pair of fields
/*
// get the start time
var sStart = this.getField('Field8').value;
// get the end time
var sEnd = this.getField('Field9').value;
// complete script only if we have data
if(sStart != '' & sEnd != '') {
// convert sStart string to seconds var fStart = Time2Num('hh:mm', sStart);
// convert sEnd string to seconds var fEnd = Time2Num('hh:mm', sEnd);
// compute difference in seconds
fDiff += fEnd - fStart;
}
*/
fDiff += TimeDiff('Field8', 'Field9');
// convert to rounded minutes if not zero
if (fDiff != 0) {
fDiff = Math.round(fDiff / 60);
// report decimal hours event.value = fDiff / 60;
}@
Copy link to clipboard
Copied
So, once you get the interval in minutes, then one only needs to divide the number of minutes by 60. You might end up with an irrational value for the field value because decimal values in many cases do not convert to rational binary decimals. This mean you need to round the value to some reasonable number of decimal places.