• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1993
  • Last Modified:

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?
  • 2
2 Solutions
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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now