Format Function Changes in Visual Basic


Format Function Changes in Visual Basic

 
The Visual Basic .NET Format function now follows the common language runtime specification for formatting data. For details on data formatting in the .NET Framework, see Formatting Types.

The following sections detail the changes Visual Basic .NET makes to the date/time, numeric, and string formats.

Date/Time Format

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 – 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."

Visual Basic .NET

In Visual Basic .NET, "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, use the DatePart function as follows:

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

"M" and "m" apply to different things, so both are now 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, use the DatePart function as follows:

Format(DatePart(DateInterval.Quarter, Now))

To display the minute as a number with or without leading zeros, use "m" or "mm". The "ttttt" format is no longer supported. "H" and "h" apply to different things, so both are now 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."

Numeric Format

Visual Basic 6.0

In Visual Basic 6.0, the Format function converts strings to numbers for you.

Negative numbers with an empty negative format string display an empty string.

Trailing decimal points are displayed.

Visual Basic 6.0 supports four format sections – positive, negative, zero, and null.

The exponent for formatting scientific notation supports the "#" placeholder.

The Short Date/Time specifier ("c"), displays a date and time in the ddddd ttttt format.

Visual Basic .NET

In Visual Basic .NET, the Format function does not convert strings to numbers for you. Thus, the first of the following two lines of code is invalid in Visual Basic .NET, while the second is valid:

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

Negative numbers with an empty negative format string display a minus sign, as in the following example:

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

Trailing decimal points are not displayed, as in the following examples:

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

Visual Basic .NET supports three format sections – positive, negative, and zero. If a non-zero value rounds to zero according to the first or second format section, the resulting zero is formatted according to the third section.

The exponent for formatting scientific notation does not support the "#" placeholder; use the "0" (zero) placeholder instead. Thus, the first of the following two lines of code is invalid in Visual Basic .NET, while the second is valid:

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

The short date/time specifier ("c") is now reserved for Currency formatting; for date formatting, use the General Date/Time ("g") format.

String Format

Visual Basic 6.0

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

Visual Basic .NET

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