CFLib.org – Common Function Library Project

dateRangesOverlap(start1, end1, start2, end2)

Last updated January 20, 2009

author

Leigh

Version: 1 | Requires: CF6 | Library: DateLib

Description:
Compares two date ranges to determine if they overlap (or intersect) by one or more days. Returns true if the two ranges overlap.

Return Values:
Returns a boolean.

Example:

<cfset firstStart = createDate(2008, 8, 1)>
<cfset firstEnd = createDate(2008, 8, 31)>
<cfset secondStart = createDate(2008, 8, 4)>
<cfset secondEnd = createDate(2008, 8, 16)>

<cfif dateRangesOverlap( firstStart, firstEnd, secondStart, secondEnd )>
    The date ranges overlap. Do something here.
<cfelse>
    The two ranges do NOT overlap. Do something else.
</cfif>

Parameters:

Name Description Required
start1 Initial date of the first range. Yes
end1 Ending date of the second range. Yes
start2 Initial date of the second range. Yes
end2 Ending date of the second range. Yes

Full UDF Source:

<!---
 Compares two date ranges to determine if they overlap by one or more days.
 
 @param start1      Initial date of the first range. (Required)
 @param end1      Ending date of the second range. (Required)
 @param start2      Initial date of the second range. (Required)
 @param end2      Ending date of the second range. (Required)
 @return Returns a boolean. 
 @author Leigh (cfsearching@yahoo.com) 
 @version 1, January 20, 2009 
--->
<cffunction name="dateRangesOverlap" returntype="boolean" output="false" hint="Returns true if two date ranges overlap by one or more days">
    <cfargument name="start1" type="date" required="true">
    <cfargument name="end1" type="date" required="true">
    <cfargument name="start2" type="date" required="true">
    <cfargument name="end2" type="date" required="true">
    <cfset var overlapFound = false>
    <cfset var datePart = "d">

    <cfif dateCompare(arguments.end1, arguments.start1, datePart) eq -1>
        <cfthrow message="End1 date cannot be earlier than start1 date">
    <cfelseif dateCompare(arguments.end2, arguments.start2, datePart) eq -1>
        <cfthrow message="End2 date cannot be earlier than start2 date">
    </cfif>
    <!--- first range starts within the second date range --->
    <cfif dateCompare(arguments.start1, arguments.start2, datePart) gte 0 and 
                dateCompare(arguments.start1, arguments.end2, datePart) lte 0>
        <cfset overlapFound = true>    
    <!--- first range ends within the second date range --->
    <cfelseif dateCompare(arguments.end1, arguments.start2, datePart) gte 0 and 
                dateCompare(arguments.end1, arguments.end2, datePart) lte 0>
        end between
        <cfset overlapFound = true>    
    <!--- first range spans the second date range --->
    <cfelseif dateCompare(arguments.start1, arguments.start2, datePart) lte 0 and 
                dateCompare(arguments.end1, arguments.end2, datePart) gte 0>
        spans        
        <cfset overlapFound = true>    
    </cfif>

    <cfreturn overlapFound>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson