Time Difference Calculation with an Exception
I am working on a vacation request form that has a field for start time and end time in h:mm tt format. I have a script that is working for calculating the difference in those fields, however, I need this have an exception and I am not sure what to add/change.
If an employee request 8:00 am to 12:00 pm the script works fine, however, if they request 8:00 am to 5:00 pm it fails because it doesn't take the hour lunch period into consideration.
Please advise.
// get the values of the fields
var sStart = this.getField("Start TimeRow1").value;
var sEnd = this.getField("End TimeRow1").value;
// clear field as a default action
event.value = "";
// compute only if we have values
if(sStart != "" && sEnd != "") {
// add a formatted date value to the time field values
sStart = "1-Jan-1970 " + sStart;
sEnd = "1-Jan-1970 " + sEnd;
// convert the date time strings to date object
var oStart = util.scand("d-mmm-yyyy h:MM tt", sStart);
var oEnd = util.scand("d-mmm-yyyy h:MM tt", sEnd);
// convert date object to milliseconds since 1-Jan-1970
var nStart = oStart.getTime();
var nEnd = oEnd.getTime();
// compute the difference in milliseconds
var nDiff = nEnd - nStart;
// convert to seconds
var nSeconds = Math.floor(nDiff / 1000);
// compute the whole hours from the minutes
var nHrs = Math.floor(nSeconds / (60 * 60));
// compute the remaining minutes
var nMins = (nSeconds / 60) % 60;
// set field value string to formatted time string
event.value = nHrs + ":" + util.printf("%,202.0f", nMins);
} // end have values
