HOWTO: Change the Short Date Format from Visual Basic

ID: Q168793

The information in this article applies to:

SUMMARY

This article presents a step-by-step example on how to programmatically change the short date format of the system's Regional Settings. Otherwise the short date format can be manually changed through the Regional Settings applet in Control Panel.

MORE INFORMATION

Step-by-Step Example

1. In a new or existing project, add a form (Form1).

2. Add a CommandButton (Command1) to the form.

3. Add the following code to the General Declarations section of Form1:

      Option Explicit

      Private Const LOCALE_SSHORTDATE = &H1F
      Private Const WM_SETTINGCHANGE = &H1A
      'same as the old WM_WININICHANGE
      Private Const HWND_BROADCAST = &HFFFF&

      Private Declare Function SetLocaleInfo Lib "kernel32" Alias _
          "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As _
          Long, ByVal lpLCData As String) As Boolean
      Private Declare Function PostMessage Lib "user32" Alias _
          "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
          ByVal wParam As Long, ByVal lParam As Long) As Long
      Private Declare Function GetSystemDefaultLCID Lib "kernel32" _
          () As Long

4. Place the following code in Command1_Click event procedure:

      Private Sub Command1_Click()
         Dim dwLCID As Long
         dwLCID = GetSystemDefaultLCID()
         If SetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, "dd-MMM-yy") _
            = False Then
            MsgBox "Failed"
            Exit Sub
         End If
         PostMessage HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0
      End Sub

5. Run the program and open the form. Click on Command1 and then exit the
   program.

6. Go to Control Panel and double-click on the Regional Settings icon.
   Select the Date tab. Note that the Short date style has been changed
   to "dd-MMM-yy."
Keywords          : kbprg kbVBp400 kbVBp500 kbhowto VB4WIN VBKBDLL VBKBWinAPI vbwin kb32bitOnly 
Version           : WINDOWS:4.0 5.0 7.0 97
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: October 2, 1997