Here is a small Lotusscript class I wrote some years ago. I use it in a number of other classes where I need to use date functionality of different kind. For example, I have a class that communicates with a FoxPro database, using a COM object. Some of the methods in that class uses XML while other just pass a few arguments to the COM object. The COM object expects the date values to be in ISO 8601 format (yyyy-mm-dd). In addition, sometimes the date comes from a field in a Notes document where they usually are stored in US format (mm/dd/yyyy), sometimes it is the current date.
So I decided to create a this class to just make the code cleaner and to avoid having to do the same conversions over and over again. This class can of course be extended with more functionality if you like.
I simply put the class in a script library called “Class.Date” and then use that script library in my other classes or agents.
Class DateClass Private dt As NotesDateTime Public ErrorMsg As String Public Sub New(value As Variant) Dim datestring As String ' *** Check what data type was passed and take actions. ' *** If value is blank or Nothing, use today's date. Select Case Typename(value) Case "EMPTY" : datestring = Format$(Today(),"Short Date") Case "STRING" : If Fulltrim(value) = "" Then datestring = Format$(Today(),"Short Date") Else datestring = value End If Case "DATE" : datestring = Cstr(value) End Select ' *** Also check that the value is a valid date If Isdate(datestring) = False Then ErrorMsg = "Class.Date:New() - '" & datestring & "' received is not a valid date." Set dt = Nothing Exit Sub End If ErrorMsg = "" Set dt = New NotesDateTime(datestring) End Sub Public Function DateOnly As String ' *** Return date-part only, in format selected by the system DateOnly = dt.DateOnly End Function Public Function DateOnlyISO As String ' *** Return date-part only, in ISO 8601 (big endian) standard format DateOnlyISO = Format$(dt.dateOnly,"yyyy-mm-dd") End Function Public Function DateOnlyUS As String ' *** Return date-part only, in US (middle-endian) format DateOnlyUS = Format$(dt.dateOnly,"mm/dd/yyyy") End Function End Class
And this is how I use the class, this is the first few lines of a function in another script library:
Public Function GetPolicyData(Byval policynumber As String, Byval lossdate As Variant) As Integer Dim DoL As DateClass Dim result As Integer Set DoL = New DateClass(lossdate) If DoL Is Nothing Then '*** Display message, including error message from DateTime class MsgBox = |Failed to initialize New DateTimeClass with LossDate "| & _ lossdate & |". | & DoL.ErrorMsg Exit Function End If '*** Call COM object with policy number and date of loss in ISO 8601 format result = object.GetPolicyData(policynumber, DOL.DateOnlyISO()) ...
There you have it. Easy, isn’t it?