0

我在 vb3 代码中有一个错误,我将日期设置为 31/12/2200 并跳转一个错误,告诉格式不像语言环境机器。

解决方案是手动设置 12/31/2200 但我感兴趣的是让机器的语言环境自动设置。

如何在 vb3 中更改为语言环境日期?

4

1 回答 1

1

我无法帮助您使用 VB3。现在我已经很多年没见过了。我可以给你一些适用于 VB5/VB6 的东西。我不知道它将如何转移到 VB3。希望如果它需要工作,您可以将其翻译成 VB3 或找到可以的人。您需要添加适当的错误处理。

Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Private Const LOCALE_USER_DEFAULT = &H400
Private Const LOCALE_SSHORTDATE = &H1F ' short date format string
Private Const LOCALE_SLONGDATE = &H20 ' long date format string

Private Sub Form_Load()
    Dim strMsg As String

    strMsg = "Short Date Format: " & FormatShortDate(DateTime.Now)
    strMsg = strMsg & vbCrLf & "Long Date Format: " & FormatLongDate(DateTime.Now)

    MsgBox strMsg

End Sub

Private Function FormatShortDate(ByVal vDate As Date) As String
    Dim strShortDateFormat As String
    Dim lngRet As Long
    Dim strReturn As String

    'Get short date format
    strShortDateFormat = Space(255)
    lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, strShortDateFormat, Len(strShortDateFormat))
    strShortDateFormat = Left(strShortDateFormat, lngRet - 1)

    strReturn = Format$(vDate, strShortDateFormat)

    FormatShortDate = strReturn

End Function

Private Function FormatLongDate(ByVal vDate As Date) As String
    Dim strLongDateFormat As String
    Dim lngRet As Long
    Dim strReturn As String

    'Get long date format
    strLongDateFormat = Space(255)
    lngRet = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLONGDATE, strLongDateFormat, Len(strLongDateFormat))
    strLongDateFormat = Left(strLongDateFormat, lngRet - 1)

    strReturn = Format$(vDate, strLongDateFormat)

    FormatLongDate = strReturn

End Function
于 2017-01-20T15:46:54.077 回答