Date range overlap - number of days

Dear experts -
There must be a function out there somewhere, but I can't find it -
I have two date ranges - with a start and an end date to each.
I would like to calculate the number of calendar days OVERLAP between the two ranges.
any ideas?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If the StartDt of range 2 is greater than the EndDt of range1, there is no overlap.  The same is true if the StartDt of range1 is greater than the EndDt of of range 2.  So you can just use this formula:

IFF(StartDt2>EndDt1 OR StartDt1>EndDt2, 0, DateDiff("d",IFF(StartDt2<StartDt1, StartDt1, StartDt2), IIF(EndDt2>EndDt1, EndDt1, EndDt2)))

Open in new window

Dale FyeCommented:
Well, there are six combinations of how these could overlap.  Let me lay that out for you:

Range 1:                         |                         |
#1 Range 2:    !        !                                                     starts before ends before
#2 Range 2:    !                            !                                 starts before, ends during
#3 Range 2:                       !                                     !     starts before, ends after
#4 Range 2:                                 !             !                   starts during, ends during
#5 Range 2:                                 !                           !     starts during, ends after
#6 Range 2:                                                          !     !  starts after, ends after

If you look at this analytically, the "overlap" cannot start prior to the maximum of the two start dates, and must end on the minimum of the two end dates.  

However, in two of the possibilities displayed above (#1, and #6) the Max(StartDate1, StartDate2) > Min(EndDate1, EndDate2), so there is no overlap.  So, using DateDiff, we should be able to determine the overlap:
Public Function fnOverlap(SD1 as date, ED1 as date, _
                          SD2 as date, ED2 as Date) as integer

    Dim MaxSD as Date, MinED as Date

    MaxSD = IIF(SD1 > SD2, SD1, SD2)
    MinED = IIF(ED1 < ED2, ED1, ED2)

    if MaxSD < MinED Then
        fnOverLap = 0
        fnOverLap = DateDiff("d", MaxSD, MinED)
        'Depending on how you want to count overlap you might want to use
        'fnOverLap = DateDiff("d", MaxSD, MinED) + 1
    End If

End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
terpsichoreAuthor Commented:
wow - both eloquent and insightful. this will help me get there.
Dale FyeCommented:
BTW,  using the function will allow to insert some remarks and allow anyone who looks at the code six months or a year from now to figure out exactly what you were trying to do.  Not so easy when you glom it all into a single DateDiff() function.

On the other hand, if your are using this in VBA rather than a query, you could simply document the code either way.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.