Detecting overlapping datetime ranges

So, I was working on this method for detecting overlapping time periods for approx 11 hours. The objective was at first simple: take two timeranges and compare them. If they overlap then shout out loud, if not swallow and get on with your miserable life. I must say that this was MUCH more complex than I first thought. You cannot simply check if (dtmOrigEnd > dtmNewStart ) And (dtmOrigStart  < dtmNewEnd ). What happens if the new startdate actually is lower than the old startdate, aswell as the new startdate?

Example 1:

Dim dtmOrigStart As DateTime = "2007-04-17 17:14:00"

Dim dtmOrigEnd As DateTime = "2007-04-26 19:00:00"

 

Dim dtmNewStart As DateTime = "2007-04-12 20:14:00"

Dim dtmNewEnd As DateTime = "2007-04-29 19:00:00"

If (dtmOrigStart > dtmNewStart) And (dtmOrigEnd < dtmNewEnd) Then

Console.writeline("OVERLAP!!")

End If

Well so far it seems OK, the new endDate is larger than the old one. So we have a collision. Just as I want it to be.

Example 2 where it becomes tricky:

Dim dtmOrigStart As DateTime = "2007-04-17 17:14:00"

Dim dtmOrigEnd As DateTime = "2007-04-26 19:00:00"

 

Dim dtmNewStart As DateTime = "2007-04-15 20:14:00"

Dim dtmNewEnd As DateTime = "2007-04-25 19:00:00"

If (dtmOrigStart > dtmNewStart) And (dtmOrigEnd < dtmNewEnd) Then

Console.writeline("OVERLAP!!")

End If

This will not result as an overlap. The new endDate is smaller than the old one, but the startdates are further back in time.

Sequences of days in this case:

FIRST RANGE

  

17

18

19

20

21

22

23

24

25

26

SECOND RANGE

15

16

17

18

19

20

21

22

23

24

25

 

Days marked red are overlapping days.

SOLUTION!

I did not come up with the solution, I got close but mine ended up with 50 lines of pretty much IFs. I got tired of it and asked for help at forums.asp.net.
These 6 lines of simple code work perfectly and were given to me by some guy called Tom Anker (thx!):

Private Function OverlappingPeriods(ByVal period1_start As Date, ByVal period1_end As Date, ByVal period2_start As Date, ByVal period2_end As Date)

If Date.Compare(period1_start, period2_end) <= 0 And Date.Compare(period1_end, period2_start) >= 0 Then

Return True

End If

Return False

End Function

«April»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345