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 = "");
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 表达式中包含的指令设置格式的字符串。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%")

注解

@No__t-0 方法还提供了类似的功能。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 函数将提供类似于 @no__tIf 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. 如果 Format 函数的 @no__t 参数包含预定义的数字格式之一,则只允许使用一个部分。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. 由于 Format 函数的 @no__t 参数使用字符串,因此它包含在引号中。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. 例如,以下格式使用第一部分中的格式显示正值和负值,如果值为零,则显示 ZeroFor 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. 它们可被名称用作 Format 函数的 Style 参数:These 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%.
PpP, 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.
EeE, or e 使用标准的科学记数法,并且提供六个有效位数。Uses standard scientific notation, providing six significant digits.

例如,Format(1234567, "e") 返回 1.234567e+006For example, Format(1234567, "e") returns 1.234567e+006.
DdD, or d 将数字显示为字符串,该字符串包含采用十进制(以 10 为底)格式的数字值。Displays number as a string that contains the value of the number in Decimal (base 10) format. ByteShortIntegerLong)支持此选项。This option is supported for integral types (Byte, Short, Integer, Long) only.

例如,Format(&H7F, "d") 返回 127For example, Format(&H7F, "d") returns 127.
XxX, or x 将数字显示为字符串,该字符串包含采用十六进制(以 16 为底)格式的数字值。Displays number as a string that contains the value of the number in Hexadecimal (base 16) format. ByteShortIntegerLong)支持此选项。This option is supported for integral types (Byte, Short, Integer, Long) only.

例如,Format(127, "x") 返回 7fFor example, Format(127, "x") returns 7f.
Yes/No 如果 number 为0,则显示 No;否则,将显示 YesDisplays No if number is 0; otherwise, displays Yes.

例如,Format(0, "Yes/No") 返回 NoFor example, Format(0, "Yes/No") returns No.
True/False 如果 number 为0,则显示 False;否则,将显示 TrueDisplays False if number is 0; otherwise, displays True.

例如,Format(1, "True/False") 返回 TrueFor example, Format(1, "True/False") returns True.
On/Off 如果 number 为0,则显示 Off;否则,将显示 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 Numeric Formats

下表标识了可用于创建用户定义的数字格式的字符。The following table identifies characters you can use to create user-defined number formats. 这些函数可用于生成 Format 函数的 Style 参数:These may be used to build the Style argument for the Format function:

字符Character 说明Description
NoneNone 显示不带格式的数字。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 位数占位符,不同之处在于,如果数字的位数少于格式表达式中小数点分隔符任意一侧的 @no__t 1 个字符,则不会显示前导零和尾随零。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. 如果格式表达式仅包含此符号左侧的 @no__t 0 个字符,则为; 否则为。小于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. 如果格式包含一个由数字占位符(@no__t 0 或 #)包围的千位分隔符,则指定千位分隔符的标准用法。Standard use of the thousand separator is specified if the format contains a thousand separator surrounded by digit placeholders (0 or #).

一个千位分隔符紧挨小数点分隔符的左侧(无论是否指定小数)或者作为字符串中最右侧的字符表示“通过除以 1000 来将数字按比例减小,并按需要四舍五入”。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,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:

- @ no__t,它使用千位分隔符将数字100000000的格式设置为字符串 "100000000"。- "#,0.", which uses the thousands separator to format the number 100 million as the string "100,000,000".
- @ no__t-1,它使用系数为1000的缩放来将数字100000000的格式设置为字符串 "100000"。- "#0,.", which uses scaling by a factor of one thousand to format the number 100 million as the string "100000".
- @ no__t-1,它使用千位分隔符并按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. 如果格式表达式包含至少一个数字占位符(@no__t 0 或 #)到 E-E+e- 或 @no__t 5 的左侧,则该数字将以科学记数格式显示,并在数字和其指数之间插入 E 或 @no__t 7。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 (\\).

不能显示为文字字符的字符示例包括日期格式和时间格式的字符(acdhmnpqst @no__tt-12 和 3)、数字格式字符(4,5,6,7,8,逗号,和句点),字符串格式字符(9,@no__t--20,1,2,3)。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. (所有这些示例均假设您的系统区域设置为“英语 - 美国”)第一列包含 Format 函数的 @no__t 参数的格式字符串;如果带格式的数据具有列标题中给定的值,则其他列包含生成的输出。(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.

Format (StyleFormat (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. 它们可被名称用作 Format 函数的 style 参数:These may be used by name as the style argument for the Format function:

格式名称Format Name 说明Description
General DateGGeneral 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 DatedShort Date, or d 使用您的当前区域性的短日期格式显示日期。Displays a date using your current culture's short date format. 例如 3/12/2008For example, 3/12/2008.

@No__t 0 字符以用户定义的日期格式显示日期。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.

@No__t-0 字符显示以用户定义的时间格式使用12小时制的区域设置 AMPM 值。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.
MmM, m 显示日期的月份和月中哪一天。Displays the month and the day of a date. 例如 March 12For example, March 12.

@No__t 0 字符以用户定义的日期格式显示月份。The M character displays the month in a user-defined date format. @No__t 0 字符以用户定义的时间格式显示分钟。The m character displays the minutes in a user-defined time format.
RrR, r 根据 @no__t 的属性设置日期的格式。Formats 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. 在调用 @no__t 0 函数之前,必须将日期/时间值调整为 GMT。You 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.

@No__t 0 字符以用户定义的时间格式显示秒。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.
YyY, y 将日期设置为年和月的格式。Formats the date as the year and month. 例如 March, 2008For example, March, 2008.

@No__t 0 和 @no__t 1 字符以用户定义的日期格式显示年份。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 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). 如果这是用户定义的数字格式中的唯一字符,则使用 %dUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %MUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %hUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %HUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %mUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %sUse %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. 你最多可以在用户定义的格式中使用7个 @no__t 0 个符号。You may use up to seven f symbols in your user-defined format. 如果这是用户定义的数字格式中的唯一字符,则使用 %fUse %f if this is the only character in your user-defined numeric format.
t 使用12小时制时钟,对于中午之前的任何小时都显示一个大写 A;在中午到 11:59 P.M 之间的任何小时显示大写 PUses 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. 如果这是用户定义的数字格式中的唯一字符,则使用 %tUse %t if this is the only character in your user-defined numeric format.
tt 对于使用12小时制时钟的区域设置,在中午之前的任何小时都显示一个大写 AM;显示一个大写 PM,并在中午到 11:59 P.M 之间的任何小时。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. 如果这是用户定义的数字格式中的唯一字符,则使用 %yUse %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). 如果这是用户定义的数字格式中的唯一字符,则使用 %zUse %z if this is the only character in your user-defined numeric format.
zz 显示带前导零的时区偏移量(例如 -08Displays the timezone offset with a leading zero (for example, -08)
zzz 显示完整的时区偏移量(例如 -08:00Displays the full timezone offset (for example, -08:00)

旧式代码示例Legacy Code Example

下面是 @no__t 的用户定义日期和时间格式的示例:The 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.

适用于

另请参阅