Compare Two Dates
The purpose of this subroutine is to compare two dates. This code may be useful when performing field validation for a form or to check if the selected date is in the past. For example, you may want to compare today's date with a date on a form. If the date is in the past, you could display a warning message.
How It Works
Two date values are assigned to the DateTime1 and DateTime2 objects. In the first example, DateTime1 holds the current calendar date while DateTime2 references a field on a form. After they are assigned, the dates can be compared using the IF statement. It's important to understand that both date/time values must be strings. When using hard-coded dates, the value must be enclosed in double quotes (in the format mm/dd/yyyy). When using a field, the retrieved field value must also be a string.
Use the TEXT property to ensure that the returned field value is a string. In the following examples, the dateTime2 statement could be replaced with the following to ensure that the returned value equates to a string.
Set dateTime2=New NotesDateTime(doc.GetFirstItem("FIELD").text)
ImplementationExample 1
Date checks are often implemented in data validation and could be added to a QuerySave event or other event within a form, agent, or view. To implement a date comparison, assign a date value to both date objects.
Dim w As New NotesUIWorkspace Dim s As New NotesSession Dim db As NotesDatabase Dim uidoc As NotesUIDocument Dim doc As NotesDocument Set s = New NotesSession Set db = s.CurrentDatabase Set uidoc = w.CurrentDocument Set doc = uidoc.Document Dim dateTime1 As NotesDateTime Dim dateTime2 As NotesDateTime ' Both values MUST be a STRING. Set dateTime1 = New NotesDateTime( "Today" ) Set dateTime2 = New NotesDateTime( doc.FIELD(0) ) If dateTime2.DateOnly < dateTime1.DateOnly Then Msgbox "Date1 is greater than Date2" Elseif dateTime2.DateOnly > dateTime1.DateOnly Then Msgbox "Date1 is less than Date2" Else Msgbox "Date1 equals Date2" End If
ImplementationExample 2
Alternatively, you may require a minimum number of days for a user-specified date. Let's say you have a form with a "desired completion date" field and you require 14 days for all service request documents. Using the AdjustDay method, you could compare the dates. If the date is less than the adjusted date, a warning message is displayed.
Dim w As New NotesUIWorkspace Dim s As New NotesSession Dim db As NotesDatabase Dim uidoc As NotesUIDocument Dim doc As NotesDocument Set s = New NotesSession Set db = s.CurrentDatabase Set uidoc = w.CurrentDocument Set doc = uidoc.Document Dim dateTime1 As NotesDateTime Dim dateTime2 As NotesDateTime Set dateTime1 = New NotesDateTime( "Today" ) Call dateTime1.AdjustDay( 14 ) Set dateTime2 = New NotesDateTime( doc.FIELD(0) ) If dateTime2.DateOnly < dateTime1.DateOnly Then Msgbox "2 weeks advance notice is required for all requests" ' continue = false ' Uncomment to halt the document save ' Exit Sub ' Uncomment to exit event Else Msgbox "Thank you for advance notice." End If
Tip
To implement this technique as part of data validation for a form, add the previous code to the form's QuerySave event and set Continue to false if the values do not pass the validation check. This will halt execution of the document save. To halt execution of the QuerySave code, you could also include an Exit Sub statement after displaying the validation error message. See additional information later on data validation. Both statements are included in the code. To implement, uncomment the appropriate statement.
Tip
To compare two date fields on a form, replace the value stored in DateTime1 with a field reference. For example, change
Set dateTime1 = New NotesDateTime("Today")
to reference a field on the form
Set dateTime1 = New NotesDateTime(doc.FIELD(0))