Search code examples
vb6vb5

vb3: change to locale date format


I have an error in vb3 code which I set date as 31/12/2200 and jump an error telling the format is not like locale machine.

The solution is to set manually 12/31/2200 but what I am interested is to get the locale of the machine to make it automatically.

how can I change to locale date in vb3?


Solution

  • I can't help you with VB3. I haven't seen that in many years now. I can give you something that will work in VB5/VB6. I do not know how well it will transfer to VB3. Hopefully if it needs work you can translate it to VB3 or find someone that can. You'll want to add appropriate error handling.

    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