Date Comparison Script to Verify a Date Is Between Two Others (a Range)

Mindwatering Incorporated

Author: Tripp W Black

Created: 10/08/2003 at 09:40 PM

 

Category:
Notes Developer Tips
LotusScript, Views

Issue:
Need to determine if a date is in between two other dates.

Solution A:
Use TimeDifferenceDouble function:

Function IsInTimeRange(s As NotesSession, timestart As String, timeend As String) As Integer
' checks to see if time passed in time range, returns one for success/true (e.g. 09:01:00 and 23:25:00)
Dim startNDT As NotesDateTime
Dim endNDT As NotesDateTime
Dim nowNDT As New NotesDateTime(Now)

On Error Goto FErrorHandler

' do basic sanity check
If (timestart="" Or timeend="") Then
IsInTimeRange = 0
Exit Function
End If

' set times
Set startNDT = New NotesDateTime(nowNDT.DateOnly & " " & timestart)
Set endNDT = New NotesDateTime(nowNDT.DateOnly & " " & timeend)
If (startNDT Is Nothing Or endNDT Is Nothing) Then
IsInTimeRange=0
Exit Function
End If
Print "Start: " & Cstr(startNDT.LocalTime)
Print "End: " & Cstr(endNDT.LocalTime)

' check if now after start time
Print "start: " & Cstr(nowNDT.TimeDifferenceDouble( startNDT ))
Print "end: " & Cstr(nowNDT.TimeDifferenceDouble( endNDT ))
If (nowNDT.TimeDifferenceDouble( startNDT ) > 0) Then
Print "After startNDT today (" & Cstr(startNDT.LocalTime) & ") . . ."
Else
IsInTimeRange=0
Exit Function
End If
If (nowNDT.TimeDifferenceDouble( endNDT ) < 0) Then
Print "Before endNDT today (" & Cstr(endNDT.LocalTime) & "). . ."
Else
IsInTimeRange=0
Exit Function
End If

' return true
Print ". . . within time range."
IsInTimeRange=1

ErrorExit:
' return
Exit Function

FErrorHandler:
Print "IsInTimeRange-Error: " & Cstr(Erl) & ", " & Error$ & "."
Resume ErrorExit
End Function


Solution B:
The following is (in my opinion) a more logical way of comparing dates than using the .TimeDifference or TimeDiffereneDouble method. The sample code below creates two time objects and makes them a NotesDateRange. It then compares the datetime of a third object checking to see if it is within the range dates.


Dim dateBegin As NotesDateTime ' beginning date of range to return
Dim dateEnd As NotesDateTime ' ending date of range to return
Dim dateRange As NotesDateRange ' range of time for comparison to 2nd dateDoc
Dim dateDoc As NotesDateTime ' compare date in dateDoc
Dim dateLS As Variant ' dateDoc as a LS Variant

...
Set doc=s.DocumentContext
...

Set dateBegin = New NotesDateTime(doc.DateBegin(0))
Set dateEnd = New NotesDateTime(doc.DateEnd(0))
Set dateRange = s.CreateDateRange()
Set dateRange.StartDateTime = dateBegin
Set dateRange.EndDateTime = dateEnd

...
Set e = x.GetFirstEntry
...
Set dateDoc = New NotesDateTime(e.ColumnValues(0))
If Not (dateDoc Is Nothing) Then
dateLS = dateDoc.LSLocalTime ' get the local time LS time variant object
If ((dateLS >= dateRange.StartDateTime.LSLocalTime) And (dateLS <= dateRange.EndDateTime.LSLocalTime)) Then
' add data since within dates requested
...
do whatever
...
End If
End If

previous page