About This Code
Brief Description:
Determine if a Date/Time Range overlaps another Date/Time Range
Type:
Date/Time functions
Notes Version:
R6.x, R5.x, R7.x
Last Modified:
01 Jun 2006
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
I was working on a project that required me to have the ability in LS to check for Date/Time Range overlaps & I could not find anything out there quickly enough so I wrote this to take care of it.
Hope this helps someone else ;o)
'EXAMPLE SCENARIOS:
'S1 = StartDateTime 1
'S2 = StartDateTime 2
'E1 = EndDateTime 1
'E2 = EndDateTime 2
'Timeline Overlaps indicated by: "*" ' s
'____________________________________________________________
'|.+S2+-+E2+-+S2+-+S1+E2+-+S2+-+E2+-+S2+E1+E2+-+S2+-+E2+-+-+.|
'|.....|_OK_|.........|_X__/|\****|.........|**.X**|...........|**/|\_X_|...........|_OK_|...........|
'|......................................|_____Main Item_______|...........................................|
'|_______________________________________________________|
Usage / Example
Function checkRange(sDT1 As NotesDateTime, eDT1 As NotesDateTime,_
sDT2 As NotesDateTime, eDT2 As NotesDateTime)
'---------------------------------------------------------------
'Jonathan Hart - PCLP R5, ND 6/6.5/7.x
'Hope this helps someone out -- Let me know if you improve this & I will update my example @ OpenNTF.org & give credit!
'Get more open source code examples at OpenNTF.org!!!
'---------------------------------------------------------------
'Function requires four date/time values passed as notesdatetime
'See example of possible scenarios above on OpenNTF.org
Dim dTR1 As NotesDateRange ' range of time to compare with 2nd range
Dim dTR2 As NotesDateRange ' range of time to compare with 1st range
Dim S1 As Variant, E1 As Variant, S2 As Variant, E2 As Variant, tmpStr As String
Dim f_S As New NotesSession
'Create First Date/Time Range
Set dTR1 = f_S.CreateDateRange()
Set dTR1.StartDateTime = sDT1
Set dTR1.EndDateTime = eDT1
'Create Second Date/Time Range
Set dTR2 = f_S.CreateDateRange()
Set dTR2.StartDateTime = sDT2
Set dTR2.EndDateTime = eDT2
S1 = dTR1.StartDateTime.LSLocalTime
S2 = dTR2.StartDateTime.LSLocalTime
E1 = dTR1.EndDateTime.LSLocalTime
E2 = dTR2.EndDateTime.LSLocalTime
If ((S1>=S2) And (E1>=E2) And (E2>=S1)) Then
tmpStr$="Condition #1"
Else
If ((S1<=S2) And (E1>=E2)) Then
tmpStr$="Condition #2"
Else
If ((S1 <= S2) And (E1<=E2) And (E1>=S2)) Then
tmpStr$="Condition #3"
Else
tmpStr$="Passed Validation!"
End If
End If
End If
checkRange = tmpStr$
End Function
Comments
Posted by Venla Junttila on 09/21/2009 05:01:50 AMBug in Condition #2
There is a bug in the code: Doesn't work if date range 1 is completely inside date range 2. Fixed that by changing the condition 2's statement to:
If ((S1<=S2) And (E1>=E2)) Or ((S2<=S1) And (E2>=E1)) Then
Otherwise nice code, thanks!