I am trying to make a timesheet and the only thing I cannot figure out is how to subtract time to hours. Sounds simple but somehow I have not managed to do it.
My time sheet is basically calculated for Monday-Friday and the workers work once in the morning and once in the afternoon so I have two fields for each day. From1 ,To1 for the morning and From2,To2 for the afternoon. So I want to be able to calculate the hours from the morning and the afternoon and come up with hours for the day.
I tried looking for examples to get me through it but could not find it for javascript or simplified field notation.
Any help would be greatly appreciated.
The basic calculation for the morning hours is:
var sIn = this.getFiel("From1").value;
var sOut = this.getFiel("To1").value;
var fStart = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sIn)) / Min);
var fEnd = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sOut)) / Min);
var diff = 0;
if (fStart > fEnd) {
app.alert("The start time must be before the end time!");
}
else diff = Math.floor(fEnd - fStart);
event.value = Number(util.printf("%,101d", (diff / 60))) + Number((util.printf("%,102d", (diff % 60))) / (60 * 100));
Since this calculation in a generalized way will be repeated 10 times, you should use a document level functions to perform the calculations:
function PFDiff_HrMin(sIn, sOut)
{
/*
Purpose: compute the difference between the inputted time strings, hh:mm, into minutes
Inputs:
sIn - starting time in the hh:mm format
sOut - ending time in the hh:mm format
Returns: the difference in minutes or zero if invalid
*/
var fStart = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sIn)) / Min);
var fEnd = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sOut)) / Min);
var diff = 0;
if (fStart > fEnd) {
app.alert("The start time must be before the end time!");
}
else diff = Math.floor(fEnd - fStart);
return diff;
}
function Min2HrsMin (sMin) {
return util.printf("%,101d", (sMin / 60)) + ":" + util.printf("%,102d", (sMin % 60));
}
function Min2DecHrs (sMin) {
/*
Purpose: to convert the passed minutes into hours and decimal hours, 30 minutes = 0.5
Inputs:
sMin - minutes
Returns hours and minutes as a decimal number
*/
return Number(util.printf("%,101d", (sMin / 60))) + Number((util.printf("%,102d", (sMin % 60))) / (60 * 100));
}
The custom calculation for the first day will be:
// the field number for the morning time: 1, 3, 5, 7 ,9
var Day = 1
// compute the morning minutes
var DayMorning = PFDiff_HrMin(this.getFeild("From" + Day).value, this.getFeild("To" + Day).value)
// compute the afternoon minutes
var DayAfternoon = PFDiff_HrMin(this.getFeild("From" + Day + 1).value, this.getFeild("To" + Day + 1).value)
// add the morning and afternoon time and display the total hours
event.value = Min2NrsMin(DayMorning + DayAfternoon);
George Kaiser