Visual Basic Helper Routines
GetLocaleInfo: Get the System's Long Date Format
Posted:   Monday September 25, 2000
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch

A quick, simple and accurate API call that retrieves the long date format string as set in the Regional Date Settings. Works with all languages.
 BAS Module Code

 Form Code
Toss a Command button onto a form, and add the following:

Option Explicit
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const LOCALE_SLONGDATE As Long = &H20  'long date format string

Private Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long

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 Sub Command1_Click()

   MsgBox GetLongDateFormat
End Sub

Private Function GetLongDateFormat() As String

   Dim LCID As Long
  'get the locale for the user
   LCID = GetSystemDefaultLCID()
   If LCID <> 0 Then
     'return the long date format
      GetLongDateFormat = GetUserLocaleInfo(LCID, LOCALE_SLONGDATE)
   End If
End Function

Private Function GetUserLocaleInfo(ByVal dwLocaleID As Long, _
                                   ByVal dwLCType As Long) As String

   Dim sReturn As String
   Dim r As Long

  'call the function passing the Locale type
  'variable to retrieve the required size of
  'the string buffer needed
   r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))

  'if successful..
   If r Then

     'pad the buffer with r spaces
      sReturn = Space$(r)

     'and call again passing the buffer
      r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))

     'if successful (r > 0)
      If r Then

        'r holds the size of the string
        'including the terminating null
         GetUserLocaleInfo = Left$(sReturn, r - 1)

      End If

   End If

End Function
While the GetSystemDefaultLCID function retrieves the system default locale identifier, this is often inappropriate or insufficient in a networked environment or under an operating system where multiple locales have been installed. For example, it is possible for a network admin rolling out a standard image to have the user's default locale set to one differing from the base OS installation, and thus the system default locale.

In this situation Windows' provides an alternate API you can use to obtain the LCID for the current user ... GetUserDefaultLCID. Defined identically to GetSystemDefaultLCID, GetUserDefaultLCID function retrieves the user default–locale identifier and is therefore the most appropriate API to use when it is the user's locale you are interested in, rather than that of the system.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter