Dan Sorensen
Dan Sorensen

Reputation: 11753

Using Coldfusion, how do I determine if a given time span occurs during business hours (another time span)?

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;
}

Upvotes: 0

Views: 2280

Answers (3)

Cody Caughlan
Cody Caughlan

Reputation: 32758

You definitely want to create a date/time object with CreateDateTime and then compare dates/time frames that way. If you are taken input as a string from a webpage you can use ParseDateTime to convert a string representing a valid date/time format to a date/time object.

Upvotes: 0

kevink
kevink

Reputation: 1978

If you don't care about days use a 24 hour clock, treat everything as a simple number and just use greater than, less than...

 <cfif arguments.startime LT bizStart OR arguments.endtime GT bizEnd>
   <cfreturn false>
 <cfelse>
   <cfreturn true>
 </cfif>

Upvotes: 1

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

Personally I would created two date objects using CreateDateTime with same m/d/y values and compared them, since CF can do this correctly.

Upvotes: 1

Related Questions