A better CDate for VB6

后端 未结 8 727
悲哀的现实
悲哀的现实 2020-12-21 05:43

We have a a VB6 app (in a COM component) which uses CDate() to take a string and cast it to a Date, for storing in a database.

Depending on if we want the applicatio

8条回答
  •  别那么骄傲
    2020-12-21 06:18

    Another way to go:

    Public Enum abDateType
        abMDY
        abDMY
    End Enum
    
    Public Function MakeDate(ByVal dateString As String, ByVal dateType As abDateType, Optional delimiter As String = "/") As Date
        Dim strVals() As String
        Dim dtRtnVal As Date
        strVals = Split(dateString, delimiter)
        Select Case dateType
        Case abMDY
            dtRtnVal = DateSerial(strVals(2), strVals(0), strVals(1))
        Case abDMY
            dtRtnVal = DateSerial(strVals(2), strVals(1), strVals(0))
        Case Else
            Err.Raise vbObjectError, , "Unexpected date format."
        End Select
        MakeDate = dtRtnVal
    End Function
    

提交回复
热议问题