Given that a certain business is open 8am-5pm, what is the best way to verify that a given timespan falls within that range? The date doesn't matter, just need to confirm that the entire requested time range falls within business hours.
Here is my sample data I have to begin with. (I'm using cfscript here simply to be concise)
<cfscript>
// array of given timeslots to test
timeslots = arrayNew(1);
// 1st appointment, entirely within normal business hours
appointment = structNew();
appointment.begin = "10:00 AM";
appointment.end = "2:00 PM";
arrayAppend(timeslots, appointment);
// 2nd appointment, outside of normal business hours
appointment = structNew();
appointment.begin = "7:00 PM";
appointment.end = "9:00 PM";
arrayAppend(timeslots, appointment);
// 3rd appointment, kind of tricky, partly within business hours, still should fail
appointment = structNew();
appointment.begin = "3:00 PM";
appointment.end = "6:00 PM";
arrayAppend(timeslots, appointment);
</cfscript>
Now, I'll loop over that array run a validation function on each struct. For the sake of example, it could look like this:
<cfoutput>
<cfloop array="#timeslots#" index="i">
<!--- Should return 'true' or 'false' for each item --->
#myValidator(i.begin, i.end)#<br />
</cfloop>
</cfoutput>
...So back to the real question,... how do I use Coldfusion to compare those times with the given business hours?
<cfscript>
// if input data is outside of the internal time range, return false, otherwise true
function myValidator(begin, end) {
var businessStart = "8:00 AM";
var businessEnd = "5:00 PM";
var result = false;
// what kind of tests do I put here to compare the times?
}
</cfscript>
UPDATE: my solution - rewrite of myValidator from above based on the feedback of Ben Doom and kevink.
// checks if any scheduled events fall outside business hours
function duringBusinessHours(startTime, endTime) {
var result = true;
var busStart = 800;
var busEnd = 1700;
var startNum = 0;
var endNum = 0;
// convert time string into a simple number:
// "7:00 AM" -> 700 | "3:00 PM" -> 1500
startNum = timeFormat(arguments.startTime, "Hmm");
endNum = timeFormat(arguments.endTime, "Hmm");
// start time must be smaller than end time
if (startNum GTE endNum) {
result = false;
// If start time is outside of business hours, fail
} else if (startNum LT busStart OR startNum GT busEnd) {
result = false;
// If end time is outside of business hours, fail
} else if (endNum LT busStart OR endNum GT busEnd) {
result = false;
}
return result;
}