Difference between two dates

Hi,

I have a start date and an end date. I’m trying to setup a rule. If end date is less than 5 days old, a pop up box will appear asking if the is a emergency change. if yes is selected another field will be populated and date will be accepted if not user will be required to re-enter the end date. Hope this makes sense …

Code I’ve started so far

Sub Exiting(Source As Field)

Dim ws As New NotesUIWorkspace

Dim UIDoc As NotesUIDocument

Set UIDoc = ws.CurrentDocument

Set Doc = UIDoc.Document

If Doc.ImpDateField(0) = "" Or Doc.ImpChgDateRequired(0) = "" Then

	Msgbox "Please enter a date value in the Imp Change Date."

	Exit Sub

End If

Set StartItem = Doc.GetFirstItem("ImpDateField")

Set StartDateTime = StartItem.DateTimeValue

Set FinishItem = Doc.GetFirstItem("ImpChgDateRequired")

Set FinishDateTime = FinishItem.DateTimeValue

TimeDiff = FinishDateTime.TimeDifference( StartDateTime ) 

Msgbox TimeDiff/86400

If TimeDiff/86400 <= 5 Then 

	......

End If

Subject: Difference between two dates

Hi Stefan,

I’m not sure I follow exactly what you’re trying to do, but if you need to get a time difference then the following should work and be relatively easy.

Sub Exiting(Source As Field)

Dim ws As New NotesUIWorkspace

Dim vntStartDate As Variant

Dim vntEndDate As Variant

Set Doc = ws.CurrentDocument.Document

vntStartDate = Doc.startDate(0)

vntEndDate = Doc.endDate(0)

If vntEndDate - vntStartDate <= 5 Then 

	Messagebox "End date is less than 5 days after start date"

End If 

End Sub

The key points to remember are:

  • The two fields must be ‘Date/Time’ fields

  • You can also use the function ‘Now’ instead of vntEndDate (eg. “If Now - vntStartDate <= 5 Then”)

This example uses Lotusscript date variants to hold the date/time. These represent dates as the number of days since some date in the past. They can be subtracted to give the number of days difference in dates (and fraction of a day if times are included in both dates).

hth

Richard