List and Change Window's Date Formats

This sample illustrates how to list Window's current Long and Short Date formats and how to change them programmatically. It also shows how to open a Control Panel applet, Regional Settings in this case.

List and change the system's long and short dates formats.
Download Source Code

Discussion

When this form loads it lists the current Locale Language as well as the available date formats. A locale is a collection of language-related, user-preference information such as the format to use for dates.

The starting point when working with locale information is the GetSystemDefaultLCID function. A call to this API returns the system default locale identifier - an LCID.

Armed with an LCID you can retrieve info about the current locale using the GetLocaleInfo API. I get the name of the current language by calling this function with the LOCALE_SLANGUAGE flag.

Reading the available date formats is a little trickier. You need to use the EnumDateFormats API function. This callback function takes 3 parameters. First is our LCID, second is a value specifying the type of information to retrieve. DATE_LONGDATE and DATE_SHORTDATE are used to retrieve the long and short dates respectively.

The third parameter is the address of a function in our code to call each time a date format string is enumerated. In my code the my fEnumdates function is passed to the EnumDateFormats API via the AddressOf operator. Window's will call fEnumdates once for each available date format passing it the address of the date format string. Given the string's address a call to the CopyMemory API retrieves the actual date format value.

To get the long and short date formats currently in effect GetLocaleInfo is called as before. This time the LOCALE_SLONGDATE and LOCALE_SSHORTDATE are values used.

Setting a new date format is very similar to getting a date format. Instead the SetLocaleInfo function is used. However, for the change to take place and propogate through the system immediately, the PostMessage API is called to broadcast the change.

Lastly, this program lets you open the Regional Settings applet with the click of a button. It does so by shelling out to rundll32.exe. For a complete description of how this works, see my Start Control Panel Apps page.

Note: On NT you may need to close and open Regional Settings to see your changes.

Instructions

Download the source code. Press F5 to run the program.

To open the Regional Settings Control Panel applet, click the "Open Regional Settings" button.

Click the dropdowns to see the currently available long and short date formats.

Enter a new format into the textbox and see what today's date looks like in that format. To add that format to the system and put it into effect, click "Set".




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page