Share via


Visual Basic Concepts

Locale-Aware Functions

Each locale has different conventions for displaying dates, time, numbers, currency, and other information. It is not necessary to know all the conventions of your users' locales. In Visual Basic, many functions use the user's system locale, which uses the Control Panel settings provided by the operating system to automatically determine the conventions at run time. These functions are called locale-aware functions.

Even though the Print method provides little flexibility for different output formats, it does use the user's system locale. In the following example, dates are printed using the correct short date format, numbers are printed with the correct decimal separator, and currencies are printed with the correct symbol:

MyDate = #11/24/1997#
MyNumber = 26.5
Money = 1636.32
MyMoney = Format(Money, "###,###.##")
Debug.Print MyDate, MyNumber, MyMoney

When this code is run in an English/U.S. locale, the following output appears in the Immediate window:

11/24/1997   26.5   1,636.32

When this code is run in a German/Germany locale, the following output appears in the Immediate window:

24/11/1997   26,5   1.632,32

For More Information   See "Print Method" in the Language Reference.

Format Function

The Format function can accept format codes, but format codes always produce the same type of output regardless of the user's locale. For example, the format code "mm-dd-yy" is not appropriate for a user in Belgium, where the day precedes the month.

For more flexibility, the Format function also provides named formats that will automatically determine which conventions to use at run time, including General Date, Long Date, Short Date, and Long Time. Using named formats produces output that is based on the user's system locale. The named formats can even generate output in the user's native language, including the names of months and days of the week. The following example illustrates this:

MyDate = #8/22/1997 5:22:20 PM#
NewDate1 = Format(MyDate, "Medium Date")
NewDate2 = Format(MyDate, "Short Date")
NewDate3 = Format(MyDate, "Long Date")
NewDate4 = Format(MyDate, "General Date")
Debug.Print NewDate1, NewDate2, NewDate3, NewDate4

When this code is run in an English/U.S. locale, the following output appears in the Immediate window:

22-Aug-97 8/22/97      Monday, August 22, 1997   8/22/97 5:22:20 PM

When this code is run in a French/France locale, the following output appears in the Immediate window:

22-août-97   22/08/97      lundi 22 août 1997      22/08/97 17:22:20f

For More Information   See "Format Function" in the Language Reference.