vb3:更改为区域设置日期格式

时间:2017-01-20 10:37:34

标签: vb6 vb5

我在vb3代码中出错,我将日期设置为31/12/2200并跳转错误,告知格式与locale机器不同。

解决方案是手动设置12/31/2200,但我感兴趣的是获取机器的区域设置以自动生成。

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

1 个答案:

答案 0 :(得分: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
相关问题