Export (0) Print
Expand All
18 out of 58 rated this helpful - Rate this topic

Format Function for Visual Basic 6.0 Users 

The Visual Basic 2005 Format function now follows the common language runtime (CLR) specification for formatting data. For more information on data formatting in the .NET Framework, see Formatting Types.

The following sections detail the changes Visual Basic 2005 makes to the user-defined date/time, numeric, and string formats.

User-Defined Date/Time Formats

Visual Basic 6.0

In Visual Basic 6.0, to display a short or long date, you use either the "ddddd" or "dddddd" format specifier. The DayOfWeek ("w") and WeekOfYear ("ww") specifiers display the day considered to be the first day of the week, and the week considered to be the first week of the year. The lowercase "m" character displays the month as a number without a leading zero. The Quarter specifier ("q") displays the quarter of the year as a number from 1 to 4.

To display a minute as a number with or without leading zeros, you use either the "Nn" or the "N" format specifier. The characters "Hh" display the hour as a number with leading zeros, and "ttttt" displays the time as a complete time. To display either an uppercase or lowercase "A" or "P" with any hour before or after noon, you use either "AM/PM", "am/pm", "A/P", "a/p", or "AMPM."

The short date/time specifier ("c") displays a date and time in the "ddddd ttttt" format.

Visual Basic 2005

In Visual Basic 2005, "ddddd" and "dddddd" behave the same as "dddd", displaying the full name of the day. They do not display the short date and long date. DayOfWeek ("w") and WeekOfYear ("ww") are not supported. Instead, you can use the DatePart function, as the following example shows.

    Format(DatePart(DateInterval.Weekday, Now))
...    Format(DatePart(DateInterval.WeekOfYear, Now))

"M" and "m" mean different things, so they are case sensitive. Use uppercase "M" only for the month in the date portion of a date/time format, and lowercase "m" only for the minutes in the time portion.

The Quarter format specifier is not supported. Instead, you can use the DatePart function, as the following example shows.

Format(DatePart(DateInterval.Quarter, Now))

To display the minute as a number with or without leading zeros, use "mm" or "m", respectively. The "ttttt" format is no longer supported. "H" and "h" mean different things, so they are case sensitive. Use uppercase "H" only for a 24-hour clock, and lowercase "h" only for a 12-hour clock. The AM/PM formats are replaced with "t" and "tt."

The "c" specifier is used for currency formatting. For date/time formatting, use "g" for the short date/time specifier and "G" for the general date/time specifier. Both "g" and "G" use your current locale setting to determine the appropriate date and time formats.

User-Defined Numeric Formats

Visual Basic 6.0

In Visual Basic 6.0, the Format function converts strings to numbers if necessary before formatting them. Format displays a trailing decimal point if there is no fractional part.

Visual Basic 6.0 supports four sections in the formatting string. These are separated by semicolons (;) and specify how to format positive, negative, zero, and null values, respectively. Negative numbers with an empty negative section of the format string display an empty string.

Scientific notation formatting supports both the "0" and "#" digit placeholders following the exponent.

Visual Basic 2005

In Visual Basic 2005, the Format function does not convert strings to numbers before formatting. You must pass a number in the first argument, not a string. In the following example, the first line of code does not produce the intended result in Visual Basic 2005, while the second one does.

    MsgBox(Format("1.234", "#.#"))   ' Displays "#.#".
...    MsgBox(Format(CSng("1.234"), "#.#"))   ' Displays "1.2".

Trailing decimal points are not displayed, as the following example shows.

    MsgBox(Format(123, "###."))   ' Displays "123"
...    MsgBox(Format(123, "###.#"))   ' Displays "123"

Visual Basic 2005 supports three sections in the formatting string, for formatting of positive, negative, and zero values. If a non-zero value rounds to zero according to the first or second format section, it is formatted according to the third section. Negative numbers with an empty negative section of the format string display a minus sign, as the following example shows.

MsgBox(Format(-1, ";"))   ' Displays "-".

Scientific notation formatting supports only the "0" digit placeholder and not "#". In the following example, the first line of code does not produce the intended result in Visual Basic 2005, while the second one does.

    MsgBox(Format(123, "#e+#"))   ' Displays "12e+3".
...    MsgBox(Format(123, "#e+0"))   ' Displays "1e+2".

String Format

Visual Basic 6.0

In Visual Basic 6.0, you can create expressions for user-defined format strings with the @, &, <, >, and ! specifiers.

Visual Basic 2005

Visual Basic 2005 eliminates expression support for user-defined format strings, so the @, &, <, >, and ! format specifiers have no meaning and are no longer supported.

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.