How can I reformat a date stored as a string in VB?

冷暖自知 提交于 2020-02-06 11:14:16

问题


I have a date (11/1/2012) stored as a string variable called "sTemp". I want to assign this date to the string variable LessOfFiveDates in the format yyyyMMdd. I've been trying variants of the following code, but it's not working.

How can I reformat this so it will create the desired output?

If IsDate(sTemp) Then
    dtTemp = CDate(sTemp)
    LessOfFiveDates = CStr(Format(dtTemp, "yyyyMMdd"))
Else

回答1:


You supplied your own solution, upvotes for that!

In your other question, you asked for an elegant solution. An elegant solution for this problem would be the usage of the dotnet StringBuilder object. With this object, you are more flexible on your format. Because it is implemented in a string and not in code you can add the format in a configuration setup without any hassle.
Next to that, the stringbuilder will use the date based on your system locale and that is nice if you have your script run on systems with different settings for the display of date and time.

Dim sb : Set sb = createobject("System.Text.StringBuilder")
Dim d : d = "5/6/2007"

If isDate(d) Then
    call sb.AppendFormat("{0:yyyyMMdd}", cdate(d))
    LessOfFiveDates = sb.ToString()
End If



回答2:


I got it to work using this code:

If IsDate(sTemp) Then
    LessOfFiveDates = CStr(Year(sTemp) & Right("00" & Month(sTemp), 2) & Right("00" & Day(sTemp), 2))
Else



回答3:


If one has a developer license for Visual Studio 6.0 (or subproducts such as VB6) there is an easy enough way to get a Format() function in VBScript without the extreme overhead of .Net:

'Requires msstdfmt.dll, part of Visual Studio 6.0 and
'not meant for general redistribution.
Option Explicit

Class Formatter
    Private SDFMT, RS

    Private Sub Class_Initialize()
        Set SDFMT = CreateObject("MSSTDFMT.StdDataFormat")
        Set RS = CreateObject("ADODB.Recordset")
        With RS
            .Fields.Append "V", 12 'adVariant.
            .Open
            .AddNew
            Set .Fields(0).DataFormat = SDFMT
        End With
    End Sub

    Private Sub Class_Terminate()
        RS.Close
    End Sub

    Public Function Format(ByVal Value, ByVal Style)
        SDFMT.Format = Style
        With RS.Fields(0)
            .Value = Value
            Format = .Value
        End With
    End Function
End Class

Dim FMT

Set FMT = New Formatter

MsgBox FMT.Format(Now(), "yyyy-mmm-dd hh:nn:ss")
MsgBox FMT.Format(123456.789, "###,##0.00")

But you usually don't need such full generality, and you can use existing VBScript operations to get the required results:

Option Explicit

Private Function ZF2(ByVal Num)
    ZF2 = Right("0" & CStr(Num), 2)
End Function

Private Function DtFormat(ByVal Dt)
    DtFormat = CStr(Year(Dt)) & ZF2(Month(Dt)) & ZF2(Day(Dt))
End Function

MsgBox DtFormat(#3/11/2012#)

Of course none of that deals with the hazards one encounters when trying to use String values as Date values. These are subject to errors unless you are careful to use Universal Date Format (i.e. U.S. layout: MM/DD/YYYY) in your Strings and rely on implicit conversion. The CDate() function is locale-aware and can produce bogus results if fed Universal formatted dates though.



来源:https://stackoverflow.com/questions/13089014/how-can-i-reformat-a-date-stored-as-a-string-in-vb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!