Format Function

Returns a FixStr (String) value containing an expression formatted according to instructions contained in a format expression.

Syntax

Format[$]([expression[, format[, firstdayofweek[, firstweekofyear]]]])

The Format function syntax has these parts:

Part Description
expression Optional. Any valid expression.
format Optional. A valid named or user-defined format expression.
firstdayofweek Optional. A constant that specifies the first day of the week.
firstweekofyear Optional. A constant that specifies the first week of the year.

If the expression argument is omitted, is a non-initialized variable, or Null, the function returns an empty string.

Settings

The firstdayofweek argument has these settings:

Константа Значение Описание
cdbUseSystem 0 Use NLS API setting.
cdbSunday 1 Sunday (default)
cdbMonday 2 Monday.
cdbTuesday 3 Tuesday.
cdbWednesday 4 Wednesday.
cdbThursday 5 Thursday.
cdbFriday 6 Friday.
cdbSaturday 7 Saturday.

The firstweekofyear argument has these settings:

Константа Значение Описание
cdbUseSystem 0 Use NLS API setting.
cdbFirstJan1 1 Start with week in which January 1 occurs (default).
cdbFirstFourDays 2 Start with the first week that has at least four days in the year.
cdbFirstFullWeek 3 Start with the first full week of the year.

Remarks

To Format Do This
Numbers Use predefined named numeric formats or create user-defined numeric formats.
Strings Create your own user-defined string formats.
Dates and times Use predefined named date/time formats or create user-defined date/time formats.

If you try to format a number without specifying format, Format provides functionality similar to the Str function. However, positive numbers formatted as strings using Format don’t include a leading space reserved for the sign of the value; those converted using Str retain the leading space.

Named Date/Time Formats

The following table identifies the predefined date and time format names:

Format Name Description
Long Date Display a date according to your system's long date format.
Medium Date Display a date using the medium date format appropriate for the language version of the host application.
Short Date Display a date using your system's short date format.
Long Time Display a time using your system's long time format; includes hours, minutes, seconds.
Medium Time Display time in 12-hour format using hours and minutes and the AM/PM designator.
Short Time Display a time using the 24-hour format, for example, 17:45.

User-Defined Date/Time Formats

The following table identifies characters you can use to create user-defined date/time formats:

Character Description
(:) Time separator. In some locales, other characters may be used to represent the time separator. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character used as the time separator in formatted output is determined by your system settings.
(/) Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character used as the date separator in formatted output is determined by your system settings.
с Display the date as ddddd and display the time as ttttt, in that order. Display only date information if there is no fractional part to the date serial number; display only time information if there is no integer portion.
d Display the day as a number without a leading zero (1-31).
dd Display the day as a number with a leading zero (01-31).
ddd Display the day as an abbreviation (Sun-Sat).
dddd Display the day as a full name (Sunday-Saturday).
ddddd Display the date as a complete date (including day, month, and year), formatted according to your system's short date format setting. The default short date format is m/d/yy.
dddddd Display a date serial number as a complete date (including day, month, and year) formatted according to the long date setting recognized by your system. The default long date format is mmmm dd, yyyy.
w Display the day of the week as a number (1 for Sunday through 7 for Saturday).
ww Display the week of the year as a number (1-54).
m Display the month as a number without a leading zero (1-12). If m immediately follows h or hh, the minute rather than the month is displayed.
mm Display the month as a number with a leading zero (01-12). If m immediately follows h or hh, the minute rather than the month is displayed.
mmm Display the month as an abbreviation (Jan-Dec).
mmmm Display the month as a full month name (January-December).
q Display the quarter of the year as a number (1-4).
y Display the day of the year as a number (1-366).
yy Display the year as a 2-digit number (00-99).
yyyy Display the year as a 4-digit number (100-9999).
h Display the hour as a number without leading zeros (0-23).
Hh Display the hour as a number with leading zeros (00-23).
N Display the minute as a number without leading zeros (0-59).
Nn Display the minute as a number with leading zeros (00-59).
S Display the second as a number without leading zeros (0-59).
Ss Display the second as a number with leading zeros (00-59).
t t t t t Display a time as a complete time (including hour, minute, and second), formatted using the time separator defined by the time format recognized by your system. A leading zero is displayed if the leading zero option is selected and the time is before 10:00 A.M. or P.M. The default time format is h:mm:ss.
AM/PM Use the 12-hour clock and display an uppercase AM with any hour before noon; display an uppercase PM with any hour between noon and 11:59 P.M.
A/P Use the 12-hour clock and display an uppercase A with any hour before noon; display an uppercase P with any hour between noon and 11:59 P.M.
AMPM Use the 12-hour clock and display the AM string literal as defined by your system with any hour before noon; display the PM string literal as defined by your system with any hour between noon and 11:59 P.M. AMPM can be either uppercase or lowercase, but the case of the string displayed matches the string as defined by your system settings. The default format is AM/PM.

User-Defined String Formats

You can use the following characters for formatting strings. In this case the string must begin with one these characters. If you use more than one characters, (<) or (>) must go first, then (!) and then other characters. If the leading character in format is a character not listed in the table below, the result will be indefinite.

Character Description
@

Placeholder for a character from expression. Display a character from expression or a space. If expression has a character in the position where the at symbol (@) appears in the format string, display it; otherwise, display a space in that position.

Below is an example of how words are aligned to the right. The underline (_) sign is used to imitate a space. It looks as if the resulting line were filled from right to left with the help of the format argument:

Format("Wasya", "@@@@@@'s") Format("Dime", "@@@@@@'s") Format("Ruslan", "@@@@@@'s") Return: "_Wasya's" Return: "__Dime's" Return: "Ruslan's"

If the number of at signs (@) is less than the number of characters in expression, all symbols defined in expression will be displayed. The the example below the result will be the whole expression, and non-special characters are placed in the same positions, as in format.
Format("Fine Scotch Whisky", "@@@@ Old") 
Return: "Fine Old Scotch Whisky"
& Placeholder for a character from expression. Display a character from expression or nothing. If the string has a character in the position where the ampersand (&) appears, display it; otherwise, display nothing. Below is an example that aligns sentences to the left:
Format("Red", "&&&&& Hat")
Format("Green", "&&&&& Hat")
Format("Blue", "&&&&& Hat")
Return: "Red Hat"
Return: "Green Hat"
Return: "Blue Hat"

Format("7305305", "Your phonenumber is &&&&&&&&&&&&&. Isn't it ?")
Format("8(0482)266576", "Your phonenumber is &&&&&&&&&&&&&. Isn't it ?") 
Return: "Your phonenumber is 7305305. Isn't it ?"
Return: "Your phonenumber is 8(0482)266575. Isn't it ?" 
If the number of ampersand (&) characters is less than the number of characters in expression, the result is equivalent to (@) in the same case.
Format("RockRoll", "&&&&\&")
Return: "Rock&Roll" 
< Force lowercase. Display all characters in lowercase format.
> Force uppercase. Display all characters in uppercase format.
!

Force left to right fill of placeholders. The default is to fill placeholders from right to left. When the number of at signs (@) in format is greater than the number of characters in expression, the resulting expression is filled from left to right instead of right to left. That is, spaces are added to the right. Example:

Format("September","!@@@@@@@@@@@9")
Format("October",  "!@@@@@@@@@@10")
Format("November", "!@@@@@@@@@@11")
Return: "September__9"
Return: "October___10"
Return: "November__11"
If the number of at and ampersand signs (@) (&) is less than the number of signs in expression, placeholders are filled right to left, and "extra" characters are not displayed. Example:
Format("www.conceptdraw.com","!@@@")
Format("www.abc.net","!@@@")
Return: "com"
Return: "net"


User-Defined Numeric Formats

Format Name Description
None Display the number with no formatting.
(0)
Digit placeholder. Display a digit or a zero. If theexpression has a digit in the position where the 0 appears in the format string, display it; otherwise, display a zero in that position.
If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, display leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, round the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, display the extra digits without modification.
(#) Digit placeholder. Display a digit or nothing. If the expression has a digit in the position where the # appears in the format string, display it; otherwise, display nothing in that position.
This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has the same or fewer digits than there are # characters on either side of the decimal separator in the format expression.
(.) Decimal placeholder. In somelocales, a comma is used as the decimal separator. The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. If the format expression contains only number signs to the left of this symbol, numbers smaller than 1 begin with a decimal separator. To display a leading zero displayed with fractional numbers, use 0 as the first digit placeholder to the left of the decimal separator. The actual character used as a decimal placeholder in the formatted output depends on the Number Format recognized by your system.
(\) Display the next character in the format string. To display a character that has special meaning as a literal character, precede it with a backslash (\). The backslash itself isn't displayed. Using a backslash is the same as enclosing the next character in double quotation marks. To display a backslash, use two backslashes (\\).

 

Named Numeric Formats

Format Name Description
Scientific Use standard scientific notation.

 

Remarks

If you want to form a string but don't want some characters in format to be treated as special characters, put "\" before each such character.

"\" is not required for formatting numbers.

Example

Dim MyTime, MyDate, MyStr
MyTime = #17:04:23#
MyDate = #January 27, 1993#

' Returns current system time in the system-defined long time format.
MyStr = Format(Time, "Long Time")

' Returns current system date in the system-defined long date format.
MyStr = Format(Date, "Long Date")

MyStr = Format(MyTime, "h:m:s")             ' Returns "17:4:23".
MyStr = Format(MyTime, "hh:mm:ss AM/PM")    ' Returns "05:04:23 PM".
MyStr = Format(MyDate, "dddd, mmm d yyyy")  ' Returns "Wednesday,  Jan 27 1993".

' If format is not supplied, a string is returned.
MyStr = Format(23)   ' Returns "23".

' User-defined formats.
MyStr = Format(15, "She is # years old") ' Returns "She is 15 years old".
MyStr = Format(346.3, "+0.000")          ' Returns "+346.300".
MyStr = Format("HELLO", "<")             ' Returns "hello".
MyStr = Format("NewYork", ">!&&&&")	 ' Returns "YORK"
MyStr = Format("Black Sea", "&&&&& Nice")' Returns "Black Nice Sea"
MyStr = Format("32 June", "This is very strange date: &&&&&&&&&&, isn't it?")
                          'Returns  "This is very strange date: 32 June, isn't it?"

 

See Also

Type Conversion Functions