Force date to US format regardless of locale setti

2019-07-21 19:13发布

问题:

I have a VB6 program with this line:

strDate = Format(Date, "ddmmmyyyy")

I need it to always come out in this format according to the Cultural settings for Windows for English (United States):

17Jul2012

Unfortunately when the culture is set to something else, French, for example, I get this:

17juil2012

Is there any way to make the date format always use the English US formatting?

回答1:

Rather than mess about trying to enforce a culture-specific format, why not just hard code the month names into a simple function like this:

Private Function GetEnglishDate(ByVal d As Date) As String
    Dim monthNames
    monthNames = Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
    GetEnglishDate = Day(d) & monthNames(Month(d)) & Year(d)
End Function

Usage:

strDate = GetEnglishDate(myDate)


回答2:

Use the built-in Windows date formatting function:

Option Explicit

Private Type SYSTEMTIME
    wYear           As Integer
    wMonth          As Integer
    wDayOfWeek      As Integer
    wDay            As Integer
    wHour           As Integer
    wMinute         As Integer
    wSecond         As Integer
    wMilliseconds   As Integer
End Type

Private Declare Function GetDateFormat Lib "Kernel32" Alias "GetDateFormatW" ( _
    ByVal Locale As Long, _
    ByVal dwFlags As Long, _
    ByRef lpDate As SYSTEMTIME, _
    ByVal lpFormat As Long, _
    ByVal lpDateStr As Long, _
    ByVal cchDate As Long _
) As Long

Private Declare Function VariantTimeToSystemTime Lib "OleAut32.dll" ( _
    ByVal vtime As Date, _
    ByRef lpSystemTime As SYSTEMTIME _
) As Long

Private Sub Command_Click()

    ' Use French Canadian date - should display "mer., juil. 18 12" for today!
    Label.Caption = FormatDateWithLocale("ddd',' MMM dd yy", Now, 3084)

    ' Use United States date - should display "Wed, July 18 12" for today!
    Labe2.Caption = FormatDateWithLocale("ddd',' MMM dd yy", Now, 1033)

End Sub

Private Function FormatDateWithLocale(ByRef the_sFormat As String, ByVal the_datDate As Date, ByVal the_nLocale As Long) As String

    Dim uSystemTime                 As SYSTEMTIME
    Dim nBufferSize                 As Long

    ' Convert to standard Windows time format.
    If VariantTimeToSystemTime(the_datDate, uSystemTime) = 1 Then

        ' Run "GetDateFormat" just to get the size of the output buffer.
        nBufferSize = GetDateFormat(the_nLocale, 0&, uSystemTime, StrPtr(the_sFormat), 0&, 0&)

        If nBufferSize > 0 Then

            ' The buffer size includes the terminating null char, but all VB strings always include this, therefore allocate a buffer with one less character.
            ' Then rerun the GetDateFormat.
            FormatDateWithLocale = Space$(nBufferSize - 1)
            GetDateFormat the_nLocale, 0&, uSystemTime, StrPtr(the_sFormat), StrPtr(FormatDateWithLocale), nBufferSize

        End If

    End If

End Function

Just use different locale numbers (see http://www.dotnetindex.com/articles/990-List-of-Locale-ID--LCID--Values-as-Assigned-by-Microsoft.asp)

The date formats are slightly different from the VB ones (M is month):

http://msdn.microsoft.com/en-us/library/dd317787.aspx