CFLib.org – Common Function Library Project

dateRangesOverlap(start1, end1, start2, end2)

Last updated January 20, 2009

Version: 1 | Requires: ColdFusion MX | Library: DateLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<!---
 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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson