A better CDate for VB6

后端 未结 8 705
悲哀的现实
悲哀的现实 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:21

    DateAdd accepts a wide variety of inputs and outputs in the correct format.

    ThisLine =  "Tuesday, September 04, 2012 2:02 PM"
    
    i = InStr(ThisLine, ",")  ' get rid of the leading day
    
    If i > 0 Then
         TempResult = Trim(Right$(ThisLine, Len(ThisLine) - i))
    end if
    
    TempResult = DateAdd("s", 0, TempResult)
    
    0 讨论(0)
  • 2020-12-21 06:27

    I don't know of an easy solution. You could Split the input string into sub-strings by the delimiter, and then use DateSerial to recombine the year, month and hour numbers into a native VB6 Date variable. Something like this below. If you need to support a lot of locales, this could get complicated (see Bob's answer). Mind you, so would using DateTime.ParseExact.

    sInput = "1/3/71"
    Dim splt() As String
    splt = Split(sInput, "/")
    dte = DateSerial(splt(2) + 1900, splt(1), splt(0))  ' dd/mm/yy'
    
    0 讨论(0)
提交回复
热议问题