Strings.Format(Object, String) 方法

定義

傳回字串,其格式化方式是根據格式 String 運算式內包含的指令。Returns a string formatted according to instructions contained in a format String expression.

public static string Format (object? Expression, string? Style = "");
public static string Format (object Expression, string Style = "");
static member Format : obj * string -> string
Public Function Format (Expression As Object, Optional Style As String = "") As String

參數

Expression
Object

必要。Required. 任何有效的運算式。Any valid expression.

Style
String

選擇性。Optional. 有效的具名或使用者定義的格式 String 運算式。A valid named or user-defined format String expression.

傳回

String

字串,其格式化方式是根據格式 String 運算式內包含的指令。A string formatted according to instructions contained in a format String expression.

範例

此範例將示範 Format 函式的各種使用方式,以透過 String 格式及使用者定義的格式,將值格式化。This example shows various uses of the Format function to format values using both String formats and user-defined formats. 對於日期分隔符號 (/)、時間分隔符號 (:) 和 AM/PM 指示器 (ttt) 而言,系統顯示的實際格式化輸出需視程式碼使用的地區設定而定。For the date separator (/), time separator (:), and the AM/PM indicators (t and tt), the actual formatted output displayed by your system depends on the locale settings the code is using. 當時間和日期顯示在開發環境內時,會使用程式碼地區設定的簡短時間格式和簡短日期格式。When times and dates are displayed in the development environment, the short time format and short date format of the code locale are used.

注意

若為使用 24 小時制的地區設定,AM/PM 指示器 (ttt) 不會顯示任何內容。For locales that use a 24-hour clock, the AM/PM indicators (t and tt) display nothing.

Dim testDateTime As Date = #1/27/2001 5:04:23 PM#
Dim testStr As String
' Returns current system time in the system-defined long time format.
testStr = Format(Now(), "Long Time")
' Returns current system date in the system-defined long date format.
testStr = Format(Now(), "Long Date")
' Also returns current system date in the system-defined long date 
' format, using the single letter code for the format.
testStr = Format(Now(), "D")

' Returns the value of testDateTime in user-defined date/time formats.
' Returns "5:4:23".
testStr = Format(testDateTime, "h:m:s")
' Returns "05:04:23 PM".
testStr = Format(testDateTime, "hh:mm:ss tt")
' Returns "Saturday, Jan 27 2001".
testStr = Format(testDateTime, "dddd, MMM d yyyy")
' Returns "17:04:23".
testStr = Format(testDateTime, "HH:mm:ss")
' Returns "23".
testStr = Format(23)

' User-defined numeric formats.
' Returns "5,459.40".
testStr = Format(5459.4, "##,##0.00")
' Returns "334.90".
testStr = Format(334.9, "###0.00")
' Returns "500.00%".
testStr = Format(5, "0.00%")

備註

String.Format方法也會提供類似的功能。The String.Format method also provides similar functionality.

如果您要格式化未當地語系化的數值字串,您應該使用使用者定義的數值格式,以確保您能取得所要的外觀。If you are formatting a nonlocalized numeric string, you should use a user-defined numeric format to ensure that you get the look you want.

如果您嘗試將數位格式化但未指定 Style ,則函式會提供類似于函式的 Format 功能 Str ,雖然它是可感知的功能。If you try to format a number without specifying Style, the Format function provides functionality similar to the Str function, although it is internationally aware. 不過,使用函式格式化為字串的正數 Format 不包含為值的正負號保留的前置空格; 使用函式轉換的數位會 Str 保留前置空格。However, positive numbers formatted as strings using the Format function don't include a leading space reserved for the sign of the value; those converted using the Str function retain the leading space.

不同數位值的格式不同Different Formats for Different Numeric Values

數字的使用者定義格式運算式可以有一到三個區段 (以分號分隔)。A user-defined format expression for numbers can have from one to three sections separated by semicolons. 如果 Style 函數的引數 Format 包含其中一個預先定義的數值格式,則只允許一個區段。If the Style argument of the Format function contains one of the predefined numeric formats, only one section is allowed.

如果您使用If you use 以下是結果:This is the result
僅限一個區段One section only 格式運算式套用到所有的值。The format expression applies to all values.
兩個區段Two sections 第一個區段適用於正值和零,第二個則適用於負值。The first section applies to positive values and zeros; the second applies to negative values.
三個區段Three sections 第一個區段適用於正值,第二個區段適用於負值,第三個則適用於零。The first section applies to positive values, the second applies to negative values, and the third applies to zeros.

下列範例有兩個區段:第一個會定義正值和零的格式;第二個區段會定義負數值的格式。The following example has two sections: the first defines the format for positive values and zeros; the second section defines the format for negative values. 因為函式的 Style 引數 Format 接受字串,所以會以引號括住。Since the Style argument of the Format function takes a string, it is enclosed by quotation marks.

Dim style1 As String = "$#,##0;($#,##0)"

如果分號之間無內容,則會使用正值格式來列印遺失的區段。If you include semicolons with nothing between them, the missing section is printed using the format of the positive value. 例如,下列格式會使用第一個區段中的格式來顯示正值和負值, Zero 如果值為零,則會顯示。For example, the following format displays positive and negative values using the format in the first section and displays Zero if the value is zero.

Dim style2 As String = "$#,##0;;\Z\e\r\o"

預先定義的數值格式Predefined Numeric Formats

下表會識別預先定義的數值格式名稱。The following table identifies the predefined numeric format names. 這些可能會由名稱當做函數的 Style 引數使用 FormatThese may be used by name as the Style argument for the Format function:

格式名稱Format name 描述Description
General NumberGgGeneral Number, G, or g 顯示不含千位分隔符號的數位。Displays number with no thousand separator.

例如 Format(&H3FA, "g") 會傳回 1018For example, Format(&H3FA, "g") returns 1018.
CurrencyCcCurrency, C, or c 以千位分隔符號顯示數位(如果有的話)。在小數分隔符號的右邊顯示兩位數。Displays number with thousand separator, if appropriate; displays two digits to the right of the decimal separator. 輸出是以系統地區設定為基礎。Output is based on system locale settings.

例如 Format(1234567, "c") 會傳回 $1,234,567.00For example, Format(1234567, "c") returns $1,234,567.00.
FixedFfFixed, F, or f 在小數點分隔符號的左邊至少顯示一位數,並在小數點分隔符號的右邊顯示兩位數。Displays at least one digit to the left and two digits to the right of the decimal separator.

例如 Format(1234567, "f") 會傳回 1234567.00For example, Format(1234567, "f") returns 1234567.00.
StandardNnStandard, N, or n 使用千位分隔符號顯示數位,並在小數點分隔符號的左邊顯示至少一個數位,並顯示兩位數。Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.

例如 Format(1234567, "n") 會傳回 1,234,567.00For example, Format(1234567, "n") returns 1,234,567.00.
Percent 顯示乘以 100 的數字,並在右邊緊接著附加百分比符號 (%);一律在小數分隔符號的右邊顯示兩位數。Displays number multiplied by 100 with a percent sign (%) appended immediately to the right; always displays two digits to the right of the decimal separator.

例如 Format(0.4744, "Percent") 會傳回 47.44%For example, Format(0.4744, "Percent") returns 47.44%.
P,或 pP, or p 顯示乘以 100 後在右方附加百分比符號 (%),並以單一空格分隔且含有千位分節點的數字;在小數點分隔符號的右邊永遠顯示兩位數。Displays number with thousandths separator multiplied by 100 with a percent sign (%) appended to the right and separated by a single space; always displays two digits to the right of the decimal separator.

例如 Format(0.80345, "p") 會傳回 80.35 %For example, Format(0.80345, "p") returns 80.35 %.
Scientific 使用標準科學記號標記法,提供兩個有效位數。Uses standard scientific notation, providing two significant digits.

例如 Format(1234567, "Scientific") 會傳回 1.23E+06For example, Format(1234567, "Scientific") returns 1.23E+06.
E,或 eE, or e 使用標準科學記號標記法,提供 6 個有效位數。Uses standard scientific notation, providing six significant digits.

例如 Format(1234567, "e") 會傳回 1.234567e+006For example, Format(1234567, "e") returns 1.234567e+006.
D,或 dD, or d 以包含十進位 (基數為 10) 格式之數值的字串顯示數字。Displays number as a string that contains the value of the number in Decimal (base 10) format. Byte只有 (、 ShortInteger 、) 的整數類資料類型支援此選項 LongThis option is supported for integral types (Byte, Short, Integer, Long) only.

例如 Format(&H7F, "d") 會傳回 127For example, Format(&H7F, "d") returns 127.
X,或 xX, or x 以包含十六進位 (基數為 16) 格式之數值的字串顯示數字。Displays number as a string that contains the value of the number in Hexadecimal (base 16) format. Byte只有 (、 ShortInteger 、) 的整數類資料類型支援此選項 LongThis option is supported for integral types (Byte, Short, Integer, Long) only.

例如 Format(127, "x") 會傳回 7fFor example, Format(127, "x") returns 7f.
Yes/No No如果數位為0,則會顯示,否則會顯示 YesDisplays No if number is 0; otherwise, displays Yes.

例如 Format(0, "Yes/No") 會傳回 NoFor example, Format(0, "Yes/No") returns No.
True/False False如果數位為0,則會顯示,否則會顯示 TrueDisplays False if number is 0; otherwise, displays True.

例如 Format(1, "True/False") 會傳回 TrueFor example, Format(1, "True/False") returns True.
On/Off Off如果數位為0,則會顯示,否則會顯示 OnDisplays Off if number is 0; otherwise, displays On.

例如 Format(1, "On/Off") 會傳回 OnFor example, Format(1, "On/Off") returns On.

智慧型裝置開發人員注意事項Smart Device Developer Notes

Yes/NoTrue/FalseOn/Off 格式不受支援。The Yes/No, True/False, and On/Off formats are not supported.

User-Defined 數值格式User-Defined Numeric Formats

下表列出可用以建立使用者定義數字格式的字元。The following table identifies characters you can use to create user-defined number formats. 這些可以用來建立函數的 Style 引數 FormatThese may be used to build the Style argument for the Format function:

字元Character 描述Description
None 不使用格式顯示數字。Displays the number with no formatting.
(0)(0) 數字預留位置。Digit placeholder. 顯示數字或零。Displays a digit or a zero. 如果運算式在格式字串中出現零的位置有一個數位,則會顯示;否則,會在該位置顯示零。If the expression has a digit in the position where the zero appears in the format string, display it; otherwise, displays 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, displays 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, rounds 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, displays the extra digits without modification.
(#)(#) 數字預留位置。Digit placeholder. 顯示數字或不顯示。Displays a digit or nothing. 如果運算式在 # 格式字串中出現字元的位置有一個數位,則會顯示它,否則會顯示該位置中的任何內容。If the expression has a digit in the position where the # character appears in the format string, displays it; otherwise, displays nothing in that position.

此符號的運作方式就像 0 數位預留位置,但如果數位的位數少於 # 格式運算式中小數分隔符號任一側的字元,則不會顯示前置和尾端零。This symbol works like the 0 digit placeholder, except that leading and trailing zeros aren't displayed if the number has fewer digits than there are # characters on either side of the decimal separator in the format expression.
(.)(.) 小數預留位置。Decimal placeholder. 小數預留位置會決定小數分隔符號左邊和右邊顯示多少位數。The decimal placeholder determines how many digits are displayed to the left and right of the decimal separator. 如果格式運算式只包含 # 此符號左邊的字元,則小於1的數位會以小數分隔符號開頭。If the format expression contains only # characters 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 zero as the first digit placeholder to the left of the decimal separator. 在某些地區設定中,會使用逗號做為小數分隔符號。In some locales, a comma is used as the decimal separator. 在格式化輸出中作為小數預留位置使用的實際字元,取決於您系統所辨識的數字格式。The actual character used as a decimal placeholder in the formatted output depends on the number format recognized by your system. 因此,即使在使用逗號作為小數預留位置的地區設定中,您的格式也應該使用句點作為小數預留位置。Thus, you should use the period as the decimal placeholder in your formats even if you are in a locale that uses a comma as a decimal placeholder. 格式化的字串會以正確地區設定格式出現。The formatted string will appear in the format correct for the locale.
(%)(%) 百分比預留位置。Percent placeholder. 將運算式乘以 100。Multiplies the expression by 100. 百分比字元 (%) 會插入在其出現於格式字串中的位置。The percent character (%) is inserted in the position where it appears in the format string.
(,)(,) 千位分隔符號。Thousand separator. 千位分隔符會在小數分隔符號左邊有四個以上位置的數字中,分隔百位數與千位數。The thousand separator separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. 如果格式包含以數位預留位置( (或) )括住的千位分隔符號,則會指定千位分隔符號的標準用法 0 #Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #).

千位分隔符緊鄰小數分隔符號左側 (無論是否指定小數) 或為字串最右側字元,其表示「將數字除以 1,000 以調整數值,並視需要四捨五入。」A thousand separator immediately to the left of the decimal separator (whether or not a decimal is specified) or as the rightmost character in the string means "scale the number by dividing it by 1,000, rounding as needed." 小於1000但大於或等於500的數位會顯示為 1 ,而小於500的數位會顯示為 0Numbers smaller than 1,000 but greater or equal to 500 are displayed as 1, and numbers smaller than 500 are displayed as 0. 此位置的兩個相連千位分隔符號會以係數 1 百萬調整規模,而每個額外的分隔符號則另有一個係數 1,000。Two adjacent thousand separators in this position scale by a factor of 1 million, and an additional factor of 1,000 for each additional separator.

在緊鄰小數分隔符號左側或字串最右側以外任何位置中的多個分隔符號,僅視為指定使用千位分隔符號。Multiple separators in any position other than immediately to the left of the decimal separator or the rightmost position in the string are treated simply as specifying the use of a thousand separator. 在某些地區設定中,會使用句點做為千位分隔符。In some locales, a period is used as a thousand separator. 在格式化輸出中做為千位分隔符的實際字元,取決於您的系統所能辨識的數字格式。The actual character used as the thousand separator in the formatted output depends on the Number Format recognized by your system. 因此,即使在使用句號作為千位分隔符的地區設定中,您的格式也應該使用逗號點作為千位分隔符號。Thus, you should use the comma as the thousand separator in your formats even if you are in a locale that uses a period as a thousand separator. 格式化的字串會以正確地區設定格式出現。The formatted string will appear in the format correct for the locale.

例如,請設想下列三種格式字串:For example, consider the three following format strings:

- "#,0.",使用千位分隔符號將數位100000000格式化為字串 "100000000"。- "#,0.", which uses the thousands separator to format the number 100 million as the string "100,000,000".
- "#0,.",其使用以1000的比例進行調整,以將數位100000000格式化為字串 "100000"。- "#0,.", which uses scaling by a factor of one thousand to format the number 100 million as the string "100000".
- "#,0,.",使用千位分隔符號,並以1000進行調整以將數位100000000格式化為字串 "100000"。- "#,0,.", which uses the thousands separator and scaling by one thousand to format the number 100 million as the string "100,000".
(:)(:) 時間分隔符號。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.
(E-``E+``e-``e+)(E-``E+``e-``e+) 科學格式。Scientific format. 如果格式運算式在、、或的左邊至少包含一個數位預留位置 (0#) E- E+ e- e+ ,則數位會以科學格式顯示, Ee 插入數位和其指數之間。If the format expression contains at least one digit placeholder (0 or #) to the left of E-, E+, e-, or e+, the number is displayed in scientific format and E or e is inserted between the number and its exponent. 左邊數字預留位置的數目,會決定指數的數字數目。The number of digit placeholders to the left determines the number of digits in the exponent. 使用 E-e- ,將負號放在負指數旁。Use E- or e- to place a minus sign next to negative exponents. 使用 E+ 或在 e+ 正指數旁邊加上正負號,並在正指數旁邊加上加號。Use E+ or e+ to place a minus sign next to negative exponents and a plus sign next to positive exponents. 您也必須在這個符號的右邊加上數位預留位置,才能取得正確的格式。You must also include digit placeholders to the right of this symbol to get correct formatting.
- + $ ( )- + $ ( ) 常值字元。Literal characters. 這些字元在格式字串中要和鍵入的完全一致。These characters are displayed exactly as typed in the format string. 若要顯示非列出的字元,請在其前面加上反斜線 (\) 或以雙引號 (" ") 括住。To display a character other than one of those listed, precede it with a backslash (\) or enclose it in double quotation marks (" ").
(\)(\) 顯示格示字串中的下一個字元。Displays 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 (\\).

無法顯示為常值字元的字元範例包括日期格式設定和時間格式字元 (、、、、、、、、、、、、 a c d h m n p q s t w y /:) 、數值格式化字元 (、、、、、 # 逗號和 0 % E e 句號) ,以及字串格式字元 (、、、 @ & < >!) 。Examples of characters that can't be displayed as literal characters are the date-formatting and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, /, and :), the numeric-formatting characters (#, 0, %, E, e, comma, and period), and the string-formatting characters (@, &, <, >, and !).
("``ABC``")("``ABC``") 顯示雙引號 (" ") 內的字串。Displays the string inside the double quotation marks (" "). 若要從程式碼內的樣式引數中包含字串,您必須使用 Chr(34) 來括住文字 (34 是引號 ( ") ) 的字元碼。To include a string in the style argument from within code, you must use Chr(34) to enclose the text (34 is the character code for a quotation mark (")).

舊版程式碼範例Legacy Code Example

下表包含一些數字的範例格式運算式。The following table contains some sample format expressions for numbers. (這些範例都假設您系統的地區設定為 [繁體中文]。 ) 第一個資料行包含函式引數的格式字串 Style Format ; 如果格式化資料的資料行標題中有指定的值,則其他資料行會包含產生的輸出。(These examples all assume that your system's locale setting is English-U.S.) The first column contains the format strings for the Style argument of the Format function; the other columns contain the resulting output if the formatted data has the value given in the column headings.

(的格式 Style) Format (Style) "5" 格式化為"5" formatted as "-5" 格式化為"-5" formatted as "0.5" 格式化為"0.5" formatted as
Zero-length string ("") 5 -5 0.5
0 5 -5 1
0.00 5.00 -5.00 0.50
#,##0 5 -5 1
$#,##0;($#,##0) $5 ($5) $1
$#,##0.00;($#,##0.00) $5.00 ($5.00) $0.50
0% 500% -500% 50%
0.00% 500.00% -500.00% 50.00%
0.00E+00 5.00E+00 -5.00E+00 5.00E-01
0.00E-00 5.00E00 -5.00E00 5.00E-01

預先定義的日期/時間格式Predefined Date/Time Formats

下表會識別預先定義的日期和時間格式名稱。The following table identifies the predefined date and time format names. 這些可以用來當做函數的樣式引數使用 FormatThese may be used by name as the style argument for the Format function:

格式名稱Format Name 描述Description
General Date,或 GGeneral Date, or G 顯示日期及/或時間。Displays a date and/or time. 例如: 3/12/2008 11:07:31 AMFor example, 3/12/2008 11:07:31 AM. 日期顯示取決於應用程式的目前文化特性值。Date display is determined by your application's current culture value.
Long DateMedium DateDLong Date, Medium Date, or D 根據目前文化特性的完整日期格式來顯示日期。Displays a date according to your current culture's long date format. 例如: Wednesday, March 12, 2008For example, Wednesday, March 12, 2008.
Short Date,或 dShort Date, or d 使用目前文化特性的簡短日期格式來顯示日期。Displays a date using your current culture's short date format. 例如: 3/12/2008For example, 3/12/2008.

d字元會以使用者定義的日期格式顯示日期。The d character displays the day in a user-defined date format.
Long TimeMedium TimeTLong Time, Medium Time, or T 使用目前文化特性的完整時間格式來顯示時間;通常包含小時、分鐘、秒。Displays a time using your current culture's long time format; typically includes hours, minutes, seconds. 例如: 11:07:31 AMFor example, 11:07:31 AM.
Short TimetShort Time or t 使用目前文化特性的簡短時間格式顯示時間。Displays a time using your current culture's short time format. 例如: 11:07 AMFor example, 11:07 AM.

t字元 AM PM 會以使用者定義的時間格式,針對使用12小時制的地區設定顯示或值。The t character displays AM or PM values for locales that use a 12-hour clock in a user-defined time format.
f 根據目前文化特性的格式顯示完整日期和簡短時間。Displays the long date and short time according to your current culture's format. 例如: Wednesday, March 12, 2008 11:07 AMFor example, Wednesday, March 12, 2008 11:07 AM.
F 根據目前文化特性的格式顯示完整日期和完整時間。Displays the long date and long time according to your current culture's format. 例如: Wednesday, March 12, 2008 11:07:31 AMFor example, Wednesday, March 12, 2008 11:07:31 AM.
g 根據目前文化特性的格式顯示簡短日期和簡短時間。Displays the short date and short time according to your current culture's format. 例如: 3/12/2008 11:07 AMFor example, 3/12/2008 11:07 AM.
M, mM, m 顯示日期的月和日。Displays the month and the day of a date. 例如: March 12For example, March 12.

M字元會以使用者定義的日期格式顯示月份。The M character displays the month in a user-defined date format. m字元會以使用者定義的時間格式顯示分鐘。The m character displays the minutes in a user-defined time format.
R, rR, r 根據屬性將日期格式化 RFC1123PatternFormats the date according to the RFC1123Pattern property. 例如: Wed, 12 Mar 2008 11:07:31 GMTFor example, Wed, 12 Mar 2008 11:07:31 GMT. 格式化的日期不需要調整日期和時間的值。The formatted date does not adjust the value of the date and time. 您必須在呼叫函式之前,將日期/時間值調整為 GMT FormatYou must adjust the Date/Time value to GMT before calling the Format function.
s 將日期及時間格式化為可排序的索引。Formats the date and time as a sortable index. 例如: 2008-03-12T11:07:31For example, 2008-03-12T11:07:31.

s字元會以使用者定義的時間格式顯示秒。The s character displays the seconds in a user-defined time format.
u 將日期及時間格式化為 GMT 可排序的索引。Formats the date and time as a GMT sortable index. 例如: 2008-03-12 11:07:31ZFor example, 2008-03-12 11:07:31Z.
U 使用完整日期和完整時間,將日期及時間格式化為 GMT。Formats the date and time with the long date and long time as GMT. 例如: Wednesday, March 12, 2008 6:07:31 PMFor example, Wednesday, March 12, 2008 6:07:31 PM.
Y, yY, y 將日期格式化為年份和月份。Formats the date as the year and month. 例如: March, 2008For example, March, 2008.

Yy 字元會以使用者定義的日期格式顯示年份。The Y and y characters display the year in a user-defined date format.

如需應用程式目前文化特性資訊的詳細資訊,請參閱 文化特性如何影響 Visual Basic 中的字串For more information about the application's current culture information, see How Culture Affects Strings in Visual Basic.

User-Defined 日期/時間格式User-Defined Date/Time Formats

下表顯示建立使用者定義日期/時間格式的可用字元。The following table shows characters you can use to create user-defined date/time formats. 不同于舊版 Visual Basic,這些格式字元會區分大小寫。Unlike in earlier versions of Visual Basic, these format characters are case-sensitive.

字元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 that is used as the time separator in formatted output is determined by your application's current culture value.
(/)(/) 日期分隔符號。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 that is used as the date separator in formatted output is determined by your application's current culture.
(%)(%) 用以表示下列字元應讀取為單字母格式,不考慮任何尾隨字母。Used to indicate that the following character should be read as a single-letter format without regard to any trailing letters. 也可用以表示,以使用者定義的格式讀取單字母格式。Also used to indicate that a single-letter format is read as a user-defined format. 如需其他詳細資料,請參閱以下內容。See what follows for additional details.
d 將日期顯示為沒有前置零的數位 (例如 1) 。Displays the day as a number without a leading zero (for example, 1). %d如果這是使用者定義數值格式中唯一的字元,請使用。Use %d if this is the only character in your user-defined numeric format.
dd 將日期顯示為具有前置零的數位 (例如 01) 。Displays the day as a number with a leading zero (for example, 01).
ddd 將日期顯示為縮寫 (例如 Sun) 。Displays the day as an abbreviation (for example, Sun).
dddd 將日期顯示為完整名稱 (例如 Sunday) 。Displays the day as a full name (for example, Sunday).
M 以不含前置零的數字顯示月份 (例如,一月以 1) 表示。Displays the month as a number without a leading zero (for example, January is represented as 1). %M如果這是使用者定義數值格式中唯一的字元,請使用。Use %M if this is the only character in your user-defined numeric format.
MM 以包含前置零的數字顯示月份 (例如 01/12/01) 。Displays the month as a number with a leading zero (for example, 01/12/01).
MMM 將月份顯示為縮寫 (例如 Jan) 。Displays the month as an abbreviation (for example, Jan).
MMMM 將月份顯示為完整月份名稱 (例如 January) 。Displays the month as a full month name (for example, January).
gg 顯示句號/年代字串 (例如 A.D.) 。Displays the period/era string (for example, A.D.).
h 使用12小時制,將小時顯示為沒有前置零的數位 (例如 1:15:15 PM) 。Displays the hour as a number without leading zeros using the 12-hour clock (for example, 1:15:15 PM). %h如果這是使用者定義數值格式中唯一的字元,請使用。Use %h if this is the only character in your user-defined numeric format.
hh 使用12小時制,將小時顯示為前置零的數位 (例如 01:15:15 PM) 。Displays the hour as a number with leading zeros using the 12-hour clock (for example, 01:15:15 PM).
H 使用24小時制,將小時顯示為沒有前置零的數位 (例如 1:15:15) 。Displays the hour as a number without leading zeros using the 24-hour clock (for example, 1:15:15). %H如果這是使用者定義數值格式中唯一的字元,請使用。Use %H if this is the only character in your user-defined numeric format.
HH 使用24小時制,將小時顯示為前置零的數位 (例如 01:15:15) 。Displays the hour as a number with leading zeros using the 24-hour clock (for example, 01:15:15).
m 將分鐘顯示為沒有前置零的數位 (例如 12:1:15) 。Displays the minute as a number without leading zeros (for example, 12:1:15). %m如果這是使用者定義數值格式中唯一的字元,請使用。Use %m if this is the only character in your user-defined numeric format.
mm 以前置零的數字顯示分鐘 (例如 12:01:15) 。Displays the minute as a number with leading zeros (for example, 12:01:15).
s 將秒顯示為沒有前置零的數位 (例如, 12:15:5) 。Displays the second as a number without leading zeros (for example, 12:15:5). %s如果這是使用者定義數值格式中唯一的字元,請使用。Use %s if this is the only character in your user-defined numeric format.
ss 將秒顯示為前置零的數位 (例如 12:15:05) 。Displays the second as a number with leading zeros (for example, 12:15:05).
f 顯示秒的小數。Displays fractions of seconds. 例如 ff ,顯示百分之一秒,而 ffff 顯示萬分之一秒。For example ff displays hundredths of seconds, whereas ffff displays ten-thousandths of seconds. 您可以使用使用者定義格式最多七個 f 符號。You may use up to seven f symbols in your user-defined format. %f如果這是使用者定義數值格式中唯一的字元,請使用。Use %f if this is the only character in your user-defined numeric format.
t 使用12小時制,並顯示在 A 中午之前任何一小時的大寫; 顯示 P 介於中午與下午11:59 之間任何小時的大寫。Uses the 12-hour clock and displays an uppercase A for any hour before noon; displays an uppercase P for any hour between noon and 11:59 P.M. %t如果這是使用者定義數值格式中唯一的字元,請使用。Use %t if this is the only character in your user-defined numeric format.
tt 若為使用12小時制的地區設定,則會顯示 AM 在中午之前任何小時的大寫; 會顯示在 PM 中午與下午11:59 之間任何小時的大寫。For locales that use a 12-hour clock, displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M.

針對使用 24 小時制的地區設定,則不顯示任何符號。For locales that use a 24-hour clock, displays nothing.
y 顯示無前置零的年份數字 (0-9)。Displays the year number (0-9) without leading zeros. %y如果這是使用者定義數值格式中唯一的字元,請使用。Use %y if this is the only character in your user-defined numeric format.
yy 以有前置零的兩位數值格式顯示年份 (如適用)。Displays the year in two-digit numeric format with a leading zero, if applicable.
yyy 以四位數值格式顯示年份。Displays the year in four-digit numeric format.
yyyy 以四位數值格式顯示年份。Displays the year in four-digit numeric format.
z 顯示不含前置零 (的時區位移,例如 -8) 。Displays the timezone offset without a leading zero (for example, -8). %z如果這是使用者定義數值格式中唯一的字元,請使用。Use %z if this is the only character in your user-defined numeric format.
zz 顯示前置零 (的時區位移,例如, -08) Displays the timezone offset with a leading zero (for example, -08)
zzz 顯示完整的時區位移 (例如 -08:00) Displays the full timezone offset (for example, -08:00)

舊版程式碼範例Legacy Code Example

以下是使用者定義的日期和時間格式的範例 December 7, 1958, 8:50 PM, 35 secondsThe following are examples of user-defined date and time formats for December 7, 1958, 8:50 PM, 35 seconds:

格式Format 顯示Displays
M/d/yy 12/7/58
d-MMM 7-Dec
d-MMMM-yy 7-December-58
d MMMM 7 December
MMMM yy December 58
hh:mm tt 08:50 PM
h:mm:ss t 8:50:35 P
H:mm 20:50
H:mm:ss 20:50:35
M/d/yyyy H:mm 12/7/1958 20:50

智慧型裝置開發人員注意事項Smart Device Developer Notes

裝置的最小時間解析取決於裝置的製造商。The minimum time resolution for a device is determined by the manufacturer of the device. 如果裝置的時間解析為粗略,則在 f 該裝置上執行時,格式字元會傳回0。If the time resolution for the device is coarse enough, the f format character returns 0 when run on that device.

適用於

另請參閱