Q:

How to calculate the intersection of two date ranges in VBA?

I like to calculate how much one date range overlaps with another data range in VBA (MS-Access).
Sample:  

DateRangeA = 5-Jan-15 to 7-Mar-15
  DateRangeB = 3-Feb-15 to 20-June-15  

My question: How much of RangeA is part of RangeB (in percent)?
It is also possible that they don't overlap at all or RangeA is completely inside of RangeB.
I am sure I am able to write code to do this, but probably many people did this already before me and tested their code. 
So if you have ready made code then please publish an answer but don’t do the code just for me.
If nobody answers my question within 24 hours then I will write the code myself and publish it.
Thanks

A:

Here is my code which I think/hope includes all the possibilities:
Function sglGetTestOverlapsMembership(dateMembershipFrom As Date, dateMembershipTo As Date, dateTestFrom As Date, dateTestTo As Date) As Single
    Dim sglReturn As Single
    If dateMembershipTo < dateTestFrom Then
        sglReturn = 0
    ElseIf dateTestTo < dateMembershipFrom Then
        sglReturn = 0
    ElseIf dateTestFrom <= dateMembershipFrom And dateTestTo >= dateMembershipTo Then
        sglReturn = 1
    ElseIf dateMembershipFrom <= dateTestFrom And dateMembershipTo >= dateTestTo Then
        sglReturn = dateDiff("s", dateTestFrom, dateTestTo) / dateDiff("s", dateMembershipFrom, dateMembershipTo)
    ElseIf dateTestTo >= dateMembershipFrom And dateTestTo < dateMembershipTo Then
        sglReturn = dateDiff("s", dateMembershipFrom, dateTestTo) / dateDiff("s", dateMembershipFrom, dateMembershipTo)
    ElseIf dateMembershipTo >= dateTestFrom Then
        sglReturn = dateDiff("s", dateTestFrom, dateMembershipTo) / dateDiff("s", dateMembershipFrom, dateMembershipTo)
    End If
    sglGetTestOverlapsMembership = sglReturn
End Function

And this is the way to test it:
Format$(sglGetTestOverlapsMembership(#5/2/2015#, #9/2/2015#, #6/1/2015#, #7/31/2015 11:59:59 PM#), "0.00%")

A couple of remarks:
Instead of using something like DateRangeA I decided to use more descriptive variable names. What I actually want to do is find out how much of a “Test date range” is part of a “Membership date range”. 
In the dateDiff function I did not use days for the reason that it returns 0 for something like this: dateDiff("d", #1/1/2015#, #1/1/2015 23:59:59#). Using hours would have been already good but I decided to use seconds to make it as accurate as possible.
I tested it but please let me know if you see something wrong with the code. 

