Manage Learn to apply best practices and optimize your operations.

Two date functions

This tip describes two date functions.

Here are 2 general-purpose functions to have in your toolbox:
DaysBetweenThenAndNow takes in a date and returns the number of days between that date and now.
DaysBetweenTwoDates takes in 2 dates and returns the difference in days.

I use them a lot in reporting modules. In my tests they work back to 1936.


Function DaysBetweenThenAndNow 
(ThenDate As Variant) As Integer
 ' This function takes a date and 
compares it to the current date
 ' and returns the difference in days
 ' if there is an error like the date 
being passed is invalid
 '  it will return -999 as its value
 '  To use this function in your script:
 '   daysdifference = 
DaysBetweenThenAndNow (doc.SomeDate(0))
      '   where doc.SomeDate(0) 
is a date/time field
 On Error Goto errorstate
 'create varaiable to hold the calculations
 Dim diff As Double
 'get the current date
 Dim datetime As New NotesDateTime ("")
 Call datetime.SetNow()
 'get rid of the time part so that we
 are only comparing whole dates
 Call datetime.SetAnyTime
 'get the date of the passed in value
 Dim EarlierDate As New NotesDateTime
 (ThenDate)
 'get the difference between the two dates 
in seconds
 diff = datetime.TimeDifference (EarlierDate)
 ' divide by 86400 (the number of 
seconds in day) to get the number
 of days and return the value
 DaysBetweenThenAndNow = diff/86400
 Exit Function
 'in case of errors this is where we jump to
errorstate:
 DaysBetweenThenAndNow = -999
End Function

Function DaysBetweenTwoDates
 (earlierdate As Variant, laterdate As Variant) 
As Integer
 ' This function takes 2 dates and 
compares them
 ' it returns the difference in days
      ' if there is an error like the date 
being passed is invalid
 '  it will return -999 as its value
 '  To use this function in your script:
 '  daysdifference = DaysBetweenThenAndNow 
(doc.EarlierDate(0), doc.LaterDate(0))
      '   where doc.EarlierDate(0)and 
docLaterDate(0) are date/time fields
 On Error Goto errorstate
 'create varaiable to hold the calculations
 Dim diff As Double
 'get the passed in values
 Dim EarlyDate As New NotesDateTime 
(earlierdate)
 Dim LateDate As New NotesDateTime (laterdate)
 'get rid of the time part so that we are only 
comparing whole dates
 'comment these lines out if the dates 
do NOT have time components
 Call EarlyDate.SetAnyTime
 Call LateDate.SetAnyTime
 'get the difference in seconds
 diff = LateDate.TimeDifference (EarlyDate)
 'divide by the number of seconds in
 day and return the value
 DaysBetweenTwoDates = diff/86400
 Exit Function
 'this is where we go when things go wrong
errorstate:

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close