Tip

Consistent date format

If you have ever faced the dreadful way Lotus handles date formats, then you know you need a way to handle them uniformly. The following class presents a developer with an interface that gives a uniform interface to handle this.

Usage:
Include the following class in a script library.

Then to use it:

  1. Include "scriptlib name" in declaration section.
  2. Dim cd as ConsDate
To return a text string with following format:
YYYYDDMM call cd.getCommonNow

If you need other formats you could look at the getCommonNow property and play with it to fit your needs.

' Declarations
           Private n_sess As NotesSession
           Private n_datNow As NotesDateTime
           Private n_intWork As NotesInternational
           Private intDateType As Integer                                         ' NotesDate Type
           Private strDate As String                                                 ' Date to use
           Private strYear As String                                                 ' Year
           Private strMonth As String                                              ' Month
           Private strDay As String                                                  ' Day
           Private strTime As String                                                 ' Time to use
           Private strDateSep As String                                            ' Date Separator
           Private strTimeSep As String                                           ' Time Separator
           Private strAm As String                                                    ' AM identifyer
           Private strPm As String                                                    ' PM Identifyer
' END Declarations
'***************************************************************************************
' Constructor and destructor
'***************************************************************************************
           Sub New
                      Set n_sess = New NotesSession
                      Set n_datNow = New NotesDateTime(Now)
                      Me.strDate = n_datNow.DateOnly
                      Me.strTime = n_datNow.TimeOnly
                      Set n_intWork = n_sess.International
                      Me.strDateSep = Me.n_intWork.DateSep
                      Me.strTimeSep = Me.n_intWork.TimeSep
                      Me.strAm = Me.n_intWork.AMString
                      Me.strPm = Me.n_intWork.PMString

                      If Me.n_intWork.IsDateDMY Then Me.intDateType = 1
                      If Me.n_intWork.IsDateMDY Then Me.intDateType = 2
                      If Me.n_intWork.IsDateYMD Then Me.intDateType = 3
                      Call Me.InitializeDate
                      Call Me.InitializeTime
           End Sub

           Sub Delete
                      Set n_intWork = Nothing
                      Set n_sess = Nothing
                      Set n_datNow = Nothing
           End Sub

'***************************************************************************************
' Set properties
'***************************************************************************************
           Public Property Set setAmString(Byval v_strAm As String)
                      ' Changing the AM string identifyer
                      Me.strAm = v_strAm
           End Property

           Public Property Set setPmString(Byval v_strPm As String)
                      ' Changing the PM string identifyer
                      Me.strPm = v_strPm
           End Property 

           Public Property Set setTimeSep(Byval v_strTimeSep As String)
                      ' Changing the time separator used
                      Me.strTimeSep = v_strTimeSep
           End Property 

           Public Property Set setDateSep(Byval v_strDateSep As String)
                      ' Changing the date separator used
                      Me.strDateSep = v_strDateSep
           End Property

           Public Property Set setDate(Byval v_strDate As String)
                      ' Changing the date used
                      Me.strDate = v_strDate
           End Property

           Public Property Set setTime(Byval v_strTime As String)
                      ' Changing the time used
                      Me.strTime = v_strTime
           End Property

'***************************************************************************************
' Get properties
'***************************************************************************************
           Public Property Get getCommonNow As String
                      ' This is the ISO date format
                      getCommonNow = Me.strYear + "-" + Me.strMonth + "-" + Me.strDay
           End Property

           Public Property Get getTime As String
                      getTime = Me.strTime
           End Property

           Public Property Get getConvertedTime(Byval v_strTime As String) As String
                      ' Changing the time used
                      Me.strTime = v_strTime
                      Call Me.InitializeTime
                      getConvertedTime = Me.strTime
           End Property
'***************************************************************************************
' Private functions and subs
'***************************************************************************************
           Private Sub InitializeTime
                      Dim strResult As String                                            ' Result string
                      Dim intLenResult As Integer                           ' Length of result
                      Dim intLoopCounter As Integer                      ' Loop counter
                      Dim intLenFrom As Integer                            ' Length of strFrom

                      strResult = Me.strTime
                      ' We only do this if TimeSep is different from what we want
                      If Me.strTimeSep <> ":" Then
                                 intLoopCounter = 0
                                 intLenFrom = Len(Me.strTimeSep)
                                 Do While(intLoopCounter < Len(strResult))
                                            intLoopCounter = intLoopCounter + 1
                                            If Mid$(strResult, intLoopCounter, intLenFrom ) = 
Me.strTimeSep Then intLenResult=Len(strResult) strResult=Left$(strResult, (intLoopCounter-1)) + ":"
+_ Right$(strResult, intLenResult - ( (intLoopCounter-
1) + ( intLenFrom ) ) ) End If Loop End If ' initializes the time object If Instr(1,strResult,Me.strAm) <> 0 Then ' This is AM Me.strTime = Trim$(Left$(strResult,Len(strResult) -
Len(Me.strAm))) Elseif Instr(1,strResult,Me.strPm) <> 0 Then ' This Is Pm Dim intTime As Integer Dim strTemp As String strTemp = Left$(strResult,(Instr(1,strResult,":",5)-1)) strResult = Mid$(strResult,Len(strTemp) + 1) intTime = Cint(strTemp) + 12 If intTime = 24 Then intTime = 0 strResult = Cstr(intTime) + strResult Me.strTime = Trim$(Left$(strResult,Len(strResult) -
Len(Me.strPm))) Else ' This is 24H Me.strTime = strResult End If End Sub Private Sub InitializeDate ' Initialises the date object Dim intIndex As Integer intIndex = Instr(1,Me.strDate,Me.strDateSep,5) Select Case Me.intDateType Case 1 Me.strDay = Left$(Me.strDate,intIndex-1) Me.strDate = Mid$(Me.strDate,intIndex+1) intIndex = Instr(1,Me.strDate,Me.strDateSep,5) Me.strMonth = Left$(Me.strDate,intIndex-1) Me.strYear = Mid$(Me.strDate,intIndex+1) Case 2 Me.strMonth = Left$(Me.strDate,intIndex-1) Me.strDate = Mid$(Me.strDate,intIndex+1) intIndex = Instr(1,strDate,Me.strDateSep,5) Me.strDay = Left$(Me.strDate,intIndex-1) Me.strYear = Mid$(Me.strDate,intIndex+1) Case 3 Me.strYear = Left$(Me.strDate,intIndex-1) Me.strDate = Mid$(Me.strDate,intIndex+1) intIndex = Instr(1,Me.strDate,Me.strDateSep,5) Me.strMonth = Left$(Me.strDate,intIndex-1) Me.strDay = Mid$(Me.strDate,intIndex+1) End Select End Sub End Class


Do you have comments on this tip? Let us know.

This was first published in July 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.