C# String.Format formats strings

Several definitions of the String.Format method:

String.Format (String, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the specified Object instance.
String.Format (String, Object[]) Replaces the format items in the specified String with the text equivalents of the values ​​of the corresponding Object instances in the specified array.
String.Format (IFormatProvider, String, Object[]) Replaces the format items in the specified String with the text equivalents of the values ​​of the corresponding Object instances in the specified array. The specified parameters provide culture-specific formatting information.
String.Format (String, Object, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the two specified Object instances. 
String.Format (String, Object, Object, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the three specified Object instances.
 
Commonly formatted numeric result tables
 

character

illustrate

example

output

C currency string.Format("{0:C3}", 2) $2.000
D decimal string.Format("{0:D3}", 2) 002
E Scientific notation 1.20E+001 1.20E+001
G conventional string.Format("{0:G}", 2) 2
N numbers separated by semicolons string.Format("{0:N}", 250000) 250,000.00
X hexadecimal string.Format("{0:X000}", 12) C
string.Format("{0:000.000}", 12.2) 012.200

Commonly used examples
 
1. Number format of strings

 code show as below:

string str1 =string.Format("{0:N1}",56789);               //result: 56,789.0
 string str2 =string.Format("{0:N2}",56789);               //result: 56,789.00
 string str3 =string.Format("{0:N3}",56789);               //result: 56,789.000
 string str8 =string.Format("{0:F1}",56789);               //result: 56789.0
 string str9 =string.Format("{0:F2}",56789);               //result: 56789.00
 string str11 =(56789 / 100.0).ToString("#.##");           //result: 567.89
 string str12 =(56789 / 100).ToString("#.##");             //result: 567


 
2. Format currency (related to the system environment, the Chinese system formats RMB by default, and the English system formats USD)

 code show as below:

string.Format("{0:C}",0.2)

The result is: ¥0.20 (English operating system result: $0.20)
By default, two decimal places are reserved after the decimal point. If you need to reserve one or more digits, you can specify the number of digits

 code show as below:

string.Format("{0:C1}",23.15)

The result is: ¥23.2 (truncation will be automatically rounded)
to format multiple Object instances

 code show as below:

string.Format("Market price: {0:C}, preferential price {1:C}",23.15,19.82)


 
3. Format decimal numbers (formatted into a fixed number of digits, the number of digits cannot be less than before unformatted, only plastic is supported)

 code show as below:

string.Format("{0:D3}",23) //The result is: 023
string.Format("{0:D2}",1223) //The result is: 1223, (the precision specifier indicates that in the result string Minimum number of digits required.)


 
4. Numbers separated by semicolons, and specify the number of digits after the decimal point

 code show as below:

string.Format("{0:N}", 14200) //The result is: 14,200.00 (the default is two decimal places)
string.Format("{0:N3}", 14200.2458) //The result is: 14,200.246 (automatic Rounding)
 
5. Format percentage
string.Format("{0:P}", 0.24583) //The result is: 24.58% (by default, two decimal places are reserved for the percentage)
string.Format("{0:P1}", 0.24583) //The result is: 24.6% (automatic rounding)


 
6. Zero placeholders and number placeholders

 code show as below:

string.Format("{0:0000.00}", 12394.039) //The result is: 12394.04
string.Format("{0:0000.00}", 194.039) //The result is: 0194.04
string.Format("{0:## #.##}", 12394.039) //The result is: 12394.04
string.Format("{0:####.#}", 194.039) //The result is: 194


 
The following paragraph is more difficult to understand, and you can understand it by testing the actual application more.
Zero placeholder: If the formatted value has a digit where a "0" occurs in the format string, this digit is copied into the result string. The position of the leftmost "0" before the decimal point and the position of the rightmost "0" after the decimal point determine the range of numbers that will always appear in the resulting string. The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, with zero bits always being rounded off.
Digit placeholders: If the formatted value has a digit where a "#" appears in the format string, this digit is copied into the result string. Otherwise, no value is stored at this position in the result string.
Note that this specifier never displays the "0" character if "0" is not a valid number, even if "0" is the only number in the string. The "0" character is displayed if "0" is a valid digit in the displayed number. The "##" format string causes the value to be rounded to the nearest digit before the decimal point, where zeros are always dropped.
 
7. Date formatting
 

 code show as below:

string.Format("{0:d}",System.DateTime.Now) //The result is: 2009-3-20 (the month position is not 03)
string.Format("{0:D}",System.DateTime. Now) //The result is: March 20, 2009
string.Format("{0:f}",System.DateTime.Now) //The result is: March 20, 2009 15:37
string.Format(" {0:F}",System.DateTime.Now) //Result: March 20, 2009 15:37:52
string.Format("{0:g}",System.DateTime.Now) //Result It is: 2009-3-20 15:38
string.Format("{0:G}",System.DateTime.Now) //The result is: 2009-3-20 15:39:27
string.Format("{0 :m}",System.DateTime.Now) //The result is: March 20
string.Format("{0:t}",System.DateTime.Now) //The result is: 15:41
string.Format( "{0:T}",System.DateTime.Now) //The result is: 15:41:50

1. Format currency (related to the system environment, the Chinese system formats RMB by default, and the English system formats USD)

string.Format("{0:C}",0.2) The result is: ¥0.20 (English operating system result: $0.20)

By default, two decimal places are reserved after the decimal point. If you need to reserve one or more digits, you can specify the number of digits
string.Format("{0:C1}",23.15) The result is: ¥23.2 (the interception will be automatically rounded)

Format multiple Object instances
string.Format("market price: {0:C}, preferential price {1:C}",23.15,19.82)

2. Format decimal numbers (formatted into a fixed number of digits, the number of digits cannot be less than before unformatted, only plastic is supported)

string.Format("{0:D3}",23) The result is: 023

string.Format("{0:D2}",1223) The result is: 1223, (The precision specifier indicates the minimum number of digits required in the resulting string.)

3. Numbers separated by semicolons, and specify the number of digits after the decimal point

string.Format("{0:N}", 14200) The result is: 14,200.00 (the default is two decimal places)

string.Format("{0:N3}", 14200.2458) The result is: 14,200.246 (automatically rounded)

4. Format percentage

string.Format("{0:P}", 0.24583) The result is: 24.58% (by default, two decimal places are reserved for the percentage)

string.Format("{0:P1}", 0.24583) The result is: 24.6% (automatically rounded)

5. Zero placeholders and number placeholders

string.Format("{0:0000.00}", 12394.039) The result is: 12394.04

string.Format("{0:0000.00}", 194.039) The result is: 0194.04

string.Format("{0:###.##}", 12394.039) The result is: 12394.04

string.Format("{0:####.#}", 194.039) The result is: 194

The following paragraph is more difficult to understand, and you can understand it by testing the actual application more.
Zero placeholder:
If the formatted value has a digit where a "0" occurs in the format string, this digit is copied into the result string. The position of the leftmost "0" before the decimal point and the position of the rightmost "0" after the decimal point determine the range of numbers that will always appear in the resulting string.
The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, with zero bits always being rounded off.

Digit placeholders:
If the formatted value has a digit where a '#' occurs in the format string, this digit is copied into the result string. Otherwise, no value is stored at this position in the result string. 
Note that this specifier never displays the "0" character if "0" is not a valid number, even if "0" is the only number in the string. The "0" character is displayed if "0" is a valid digit in the displayed number. 
The "##" format string causes the value to be rounded to the nearest digit before the decimal point, where zeros are always dropped.

PS: space placeholder

string.Format("{0,-50}", theObj);//Format into 50 characters, the original characters are left-aligned, if insufficient, fill in spaces
string.Format("{0,50}", theObj);// Formatted into 50 characters, the original characters are right-aligned, and spaces are filled if insufficient

6. Date formatting

string.Format("{0:d}",System.DateTime.Now) The result is: 2009-3-20 (the position of the month is not 03)

string.Format("{0:D}",System.DateTime.Now) The result is: March 20, 2009

string.Format("{0:f}",System.DateTime.Now) The result is: March 20, 2009 15:37

string.Format("{0:F}",System.DateTime.Now) The result is: March 20, 2009 15:37:52

string.Format("{0:g}",System.DateTime.Now) The result is: 2009-3-20 15:38

string.Format("{0:G}",System.DateTime.Now) The result is: 2009-3-20 15:39:27

string.Format("{0:m}",System.DateTime.Now) The result is: March 20

string.Format("{0:t}",System.DateTime.Now) The result is: 15:41

string.Format("{0:T}",System.DateTime.Now) The result is: 15:41:50


For more detailed instructions, please refer to Microsoft's instructions below or check on msdn.

Microsoft MSDN's method description for string.format:

Name Description 
String.Format (String, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the specified Object instance. 
String.Format (String, Object[]) Replaces the format items in the specified String with the text equivalents of the values ​​of the corresponding Object instances in the specified array. 
String.Format (IFormatProvider, String, Object[]) Replaces the format items in the specified String with the text equivalents of the values ​​of the corresponding Object instances in the specified array. The specified parameters provide culture-specific formatting information. 
String.Format (String, Object, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the two specified Object instances. 
String.Format (String, Object, Object, Object) Replaces the format items in the specified String with the text equivalents of the values ​​of the three specified Object instances.

standard number format string

Format Specifier Name Description 
C or c
Currency
Number converted to a string representing the currency amount. Conversion is controlled by the currency format information of the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default currency precision given by the current NumberFormatInfo object is used.

D or d
Decimal number
This format is only supported for integer types. Number converted to a string of decimal digits (0-9), preceded by a minus sign if the number is negative.

The precision specifier indicates the minimum number of digits required in the resulting string. The number is left-padded with zeros, if necessary, to produce the number of digits given by the precision specifier.

E or e
scientific notation (exponent)
numbers are converted to strings of the form "-d.ddd...E+ddd" or "-d.ddd...e+ddd", where each "d" represents a digit (0 -9). If the number is negative, the string begins with a minus sign. There is always a digit before the decimal point.

The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, the default is used, which is six digits after the decimal point.

The case of the format specifier indicates whether to prefix the exponent with "E" or "e". The exponent always consists of a plus or minus sign and a minimum of three digits. Pad the exponent with zeros, if necessary, to meet the minimum three-digit requirement.

F or f
Fixed-point
numbers are converted to strings of the form "-ddd.ddd...", where each "d" represents a digit (0-9). If the number is negative, the string begins with a minus sign.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

G or g
general
The number is converted to the most compact form of fixed-point or scientific notation, depending on the number type and the presence or absence of a precision specifier. If the precision specifier is omitted or zero, the type of the number determines the default precision, as shown in the following table.

Byte 或 SByte:3

Int16 or UInt16: 5

Int32 or UInt32: 10

Int64 or UInt64: 19

Single:7

Double:15

Decimal:29

If the exponent is greater than -5 and less than the precision specifier when representing the number in scientific notation, use fixed-point notation; otherwise use scientific notation. If a decimal point is required, and trailing zeros are ignored, the result includes the decimal point. If the precision specifier is present and the result has more significands than the specified precision, the extra trailing digits are removed by rounding.

There is one exception to the above rules: if the number is Decimal and the precision specifier is omitted. In this case fixed-point notation is always used and trailing zeros are preserved.

When using scientific notation, if the format specifier is "G", the exponent of the result is prefixed with "E"; if the format specifier is "g", the exponent of the result is prefixed with "e". 

N or n
digits
Numbers are converted to strings of the form "-d,ddd,ddd.ddd...", where "-" represents the negative sign (if required), "d" represents a digit (0-9), and "," represents The thousands separator between groups of numbers, "." indicates the decimal point symbol. The actual negative number pattern, digit group size, thousands separator, and decimal separator are specified by the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

P or ppercent
Number
converted to a string representing a percentage defined by the NumberFormatInfo.PercentNegativePattern or NumberFormatInfo.PercentPositivePattern property, the former for negative cases and the latter for positive cases. Converted numbers are multiplied by 100 to express as a percentage.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

R or r
round trip
Only Single and Double types support this format. The round-trip specifier guarantees that a numeric value converted to a string is parsed as the same numeric value again. When formatting a numeric value with this specifier, it is first tested with the regular format: Double uses 15 digits of precision, Single uses 7 digits of precision. If this value is successfully parsed back to the same numeric value, it is formatted using the normal format specifiers. However, if the value is not successfully parsed as the same number, it is formatted as follows: Double uses 17 digits of precision, Single uses 9 digits of precision.

Although a precision specifier can be present here, it will be ignored. When using this specifier, round trips take precedence over precision. 

X or x
hexadecimal number
This format is only supported for integer types. A string of digits converted to hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for hexadecimal numbers greater than 9. For example, using "X" produces "ABCDEF", and using "x" produces "abcdef".

The precision specifier indicates the minimum number of digits required in the resulting string. The number is left-padded with zeros, if necessary, to produce the number of digits given by the precision specifier. 

Any other single character
(unknown specifier)
​​(The unknown specifier will throw a runtime format exception.)


custom number format string

format specifier name description 
0
zero placeholder
If the formatted value has a digit where a "0" occurs in the format string, this digit is copied into the result string. The position of the leftmost "0" before the decimal point and the position of the rightmost "0" after the decimal point determine the range of numbers that will always appear in the resulting string.

The "00" specifier causes the value to be rounded to the nearest digit before the decimal point, with zero bits always being rounded off. For example, formatting 34.5 with "00" will result in the value 35.

#Digit
Placeholder
If the formatted value has a digit where a "#" appears in the format string, this digit is copied into the result string. Otherwise, no value is stored at this position in the result string.

Note that this specifier never displays the "0" character if "0" is not a valid number, even if "0" is the only number in the string. The "0" character is displayed if "0" is a valid digit in the displayed number.

The "##" format string causes the value to be rounded to the nearest digit before the decimal point, where zeros are always dropped. For example, formatting 34.5 with "##" will result in the value 35. .decimal point The first "." character in the format string determines the position of the decimal separator in the formatted value; any other "." characters are ignored

.

The actual character used as the decimal separator is determined by the NumberDecimalSeparator property of the NumberFormatInfo that controls formatting.

,
thousands separator and numeric ratio conversion
"," character can be used as thousands separator specifier and numeric ratio conversion specifier.

Thousands separator specifier: If one or more "," characters are specified between two digit placeholders (0 or #) to format the integer digits of a number, each digit in the integer part of the output Inserts a group separator character between groups.

The NumberGroupSeparator and NumberGroupSizes properties of the current NumberFormatInfo object determine the characters used as number group separators and the size of each number group. For example, if the number 1000 is formatted using the string "#,#" and a fixed culture, the output is "1,000".

Number scaling specifiers: If one or more "," characters are specified immediately to the left of an explicit or implicit decimal point, each occurrence of a number scaling specifier divides the number being formatted by 1000. For example, if the number 1000000000 is formatted using the string "0,," the output is "100".

The thousands separator specifier and the number scale conversion specifier can be used in the same format string. For example, if you format the number 10000000000 using the string "#,0,," and a fixed culture, the output is "1,000". 

%
Percent Placeholder
The presence of a "%" character in the format string will cause the number to be multiplied by 100 before formatting. Appropriate symbols are inserted where the number itself appears in the format string at the point where a "%" occurs. The percent character used is determined by the current NumberFormatInfo class.

E0

E+0

E-0

e0

e+0

e-0
scientific notation
If any of the strings "E", "E+", "E-", "e", "e+", or "e-" appear in the format string and are immediately followed by At least one "0" character, the number is formatted in scientific notation, with an "E" or "e" inserted between the number and the exponent. The number of "0" characters following the scientific notation indicator determines the minimum number of digits for the exponent output. The "E+" and "e+" format designator characters (plus or minus) shall always precede the exponent. An "E", "E-", "e", or "e-" format indicator character precedes negative exponents only.

\
escape character
In C# and C++, the backslash character causes the next character in the format string to be interpreted as an escape sequence. It is used with traditional formatting sequences such as "\n" (newline).

In some languages, the escape character itself must follow the escape character when used as literals. Otherwise, the compiler interprets the character as an escape character. Use the string "\\" to display "\".

Note that this escape character is not supported in Visual Basic, but ControlChars provide the same functionality.

'ABC'

"ABC"
string
Characters enclosed in single or double quotes are copied into the resulting string without affecting formatting.

;
Part Separator The
";" character is used to separate the positive, negative, and zero parts of the format string.

Other
All other characters
All other characters are copied into the resulting string and do not affect formatting.


Standard DateTime format string

Format Specifier Name Description 
d
Short Date Pattern
Indicates a custom DateTime format string defined by the current ShortDatePattern property.

For example, a custom format string for a fixed culture is "MM/dd/yyyy".

D
Long Date Pattern
Represents a custom DateTime format string defined by the current LongDatePattern property.

For example, a custom format string for a fixed culture is "dddd, dd MMMM yyyy".

f
Full date/time pattern (short time)
means a combination of long date (D) and short time (t) patterns, separated by spaces.

FFull
date/time pattern (long time)
Represents a custom DateTime format string defined by the current FullDateTimePattern property.

For example, a custom format string for a fixed culture is "dddd, dd MMMM yyyy HH:mm:ss".

g
Regular date/time pattern (short time)
means a combination of short date (d) and short time (t) patterns, separated by spaces.

GGeneral
date/time pattern (long time)
means a combination of short date (d) and long time (T) patterns, separated by spaces.

M or m
MonthDayPattern
Represents a custom DateTime format string defined by the current MonthDayPattern property.

For example, a custom format string for a fixed culture is "MMMM dd".

oRound
-trip date/time pattern
Represents a custom DateTime format string using a pattern that preserves time zone information. This mode is intended for use with round-trip DateTime formats (including the Kind property as text). The formatted string can then be reverse-parsed using Parse or ParseExact with the correct Kind property value.

The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK".

The schema used for this specifier is the defined standard. Therefore, it is always the same regardless of the culture used or the format provider provided.

R or r
RFC1123 pattern
Indicates a custom DateTime format string as defined by the current RFC1123Pattern property. The schema is defined by the standard, and the property is read-only. So it is always the same regardless of the culture used or the format provider provided.

Define the format string as "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'".

Formatting does not modify the value of the DateTime object being formatted. Therefore, applications must convert the value to Coordinated Universal Time (UTC) before using this format specifier.

s
A sortable date/time pattern; conforming to ISO 8601
representing a custom DateTime format string defined by the current SortableDateTimePattern property. This schema is a defined standard, and the property is read-only. Therefore, it is always the same regardless of the culture used or the format provider provided.

The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".

tShort
TimePattern
Represents a custom DateTime format string defined by the current ShortTimePattern property.

For example, a custom format string for a fixed culture is "HH:mm".

LongTimePattern Represents a custom DateTime format string defined by the current LongTimePattern property
.

For example, a custom format string for a fixed culture is "HH:mm:ss".

uUniversal
Sortable Date/Time Pattern
Represents a custom DateTime format string defined by the current UniversalSortableDateTimePattern property. This schema is a defined standard, and the property is read-only. Therefore, it is always the same regardless of the culture used or the format provider provided.

The custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'".

No time zone conversion is done when formatting dates and times. Therefore, applications must convert the local date and time to Coordinated Universal Time (UTC) before using this format specifier.

Ugeneric
sortable date/time pattern
Represents a custom DateTime format string defined by the current FullDateTimePattern property.

This mode is the same as the full date/long time (F) mode. However, formatting will operate on Coordinated Universal Time (UTC) equivalent to the DateTime object being formatted.

Y or yYearMonthPattern
Indicates
a custom DateTime format string defined by the current YearMonthPattern property.

For example, a custom format string for a fixed culture is "yyyy MMMM".

Any other single character
(unknown specifier)
​​unknown specifier will throw a runtime format exception.


Custom DateTime format strings

The format specifier specifies that 
d
represents the day of the month as a number from 1 to 31. One-digit dates are formatted without leading zeros. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

dd
represents the day of the month as a number from 01 to 31. One-digit dates are formatted with leading zeros.

ddd
represents the abbreviated name of the day of the week as the name defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames property.

dddd (plus any number of "d" specifiers) 
represents the full name of the day of the week as defined in the current System.Globalization.DateTimeFormatInfo.DayNames property.

f
represents the most significant digit of the seconds portion.

Note that if the "f" format specifier is used alone, with no other format specifiers, it is treated as the "f" standard DateTime format specifier (full date/time pattern). For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

When using this format specifier with the ParseExact or TryParseExact methods, the number of 'f' format specifiers used indicates the most significant digits of the seconds portion to be parsed. 

ff
represents the two most significant digits of the seconds portion.

fff
represents the three most significant digits of the seconds portion.

ffff
represents the four most significant digits of the seconds portion.

fffff
represents the five most significant digits of the seconds portion.

ffffff
represents the six most significant digits of the seconds portion.

fffffff
represents the seven most significant digits of the seconds portion.

F
represents the most significant digit of the seconds portion. If this bit is zero, no information is displayed. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

When this format specifier is used with the ParseExact or TryParseExact methods, the number of 'F' format specifiers used indicates the maximum number of most significant digits of the seconds portion to be parsed.

FF
represents the two most significant digits of the seconds portion. But trailing zeros (or two zeros) are not displayed.

FFF
represents the three most significant digits of the seconds portion. But trailing zeros (or three zeros) are not displayed.

FFFF
represents the four most significant digits of the seconds portion. But trailing zeros (or four zeros) are not displayed.

FFFFF
represents the five most significant digits of the seconds portion. But trailing zeros (or five zeros) are not displayed.

FFFFFF
represents the six most significant digits of the seconds portion. But trailing zeros (or six zeros) are not displayed.

FFFFFFF
represents the seven most significant digits of the seconds portion. But trailing zeros (or seven zeros) are not displayed.

g or gg (plus any number of "g" specifiers) 
indicates a period or era (such as AD). This specifier is ignored if the date being formatted does not have an associated period or epoch string. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

h
Represents the hour as a number from 1 to 12, that is, expresses the hour by the 12-hour clock, counting whole hours from midnight or noon. Therefore, a certain number of hours after midnight cannot be distinguished from the same number of hours after noon. Hours are not rounded, and single-digit hours are formatted without leading zeros. For example, given a time of 5:43, this format specifier displays "5". For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

hh, hh (plus any number of "h" specifiers) 
Represents the hour as a number from 01 to 12, ie the hour is represented by the 12-hour clock, counting the full hour from midnight or noon. Therefore, a certain number of hours after midnight cannot be distinguished from the same number of hours after noon. Hours are not rounded, and single-digit hours are formatted with leading zeros. For example, given a time of 5:43, this format specifier displays "05".

H
represents hours as numbers from 0 to 23, ie hours are represented by a zero-based 24-hour clock, counting hours from midnight. One-digit hours are formatted without leading zeros. 

HH, HH (plus any number of "H" specifiers) 
Represents the hour as a number from 00 to 23, ie, the hour is represented by a zero-based 24-hour clock, counting hours from midnight. One-digit hours are formatted with leading zeros. 

K
represents a different value of the DateTime.Kind property, namely "Local", "Utc", or "Unspecified". This specifier cycles through the Kind values ​​as text and preserves the time zone. If the Kind value is "Local", then this specifier is equivalent to the "zzz" specifier and is used to display a local time offset, eg "-07:00". For "Utc" type values, this specifier displays the character "Z" to represent a UTC date. For values ​​of type 'Unspecified', this specifier is equivalent to "" (nothing).

m
Represents the minute as a number from 0 to 59. Minutes represent the number of whole minutes that have passed since the previous hour. One-digit minutes are formatted without leading zeros. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

mm, mm (plus any number of "m" specifiers) 
represents the minute as a number from 00 to 59. Minutes represent the number of whole minutes that have passed since the previous hour. One-digit minutes are formatted with leading zeros.

m
Represents the month as a number from 1 to 12. One-digit months are formatted without leading zeros. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

MM
represents the month as a number from 01 to 12. One-digit months are formatted with leading zeros.

MMM
represents the abbreviated name of the month as the name defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames property.

MMMM
represents the full name of the month as the name defined in the current System.Globalization.DateTimeFormatInfo.MonthNames property.

s
represents seconds as a number from 0 to 59. Seconds represents the number of whole seconds since the previous minute. One-digit seconds are formatted without leading zeros. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

ss, ss (plus any number of "s" specifiers) 
represents seconds as a number from 00 to 59. Seconds represents the number of whole seconds since the previous minute. One-digit seconds formatted with leading zeros.

t
represents the first character of the AM/PM designator defined in the current System.Globalization.DateTimeFormatInfo.AMDesignator or System.Globalization.DateTimeFormatInfo.PMDesignator property. If the hour in the time being formatted is less than 12, use the AM designator; otherwise use the PM designator. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

tt, tt (plus any number of "t" specifiers) 
represents the AM/PM designator as defined in the current System.Globalization.DateTimeFormatInfo.AMDesignator or System.Globalization.DateTimeFormatInfo.PMDesignator property. If the hour in the time being formatted is less than 12, use the AM designator; otherwise use the PM designator.

y
represents the year as up to two digits. If the year has more than two digits, only the lower two digits are shown in the result. If the year has fewer than two digits, the number is formatted without leading zeros. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

yy
represents the year as two digits. If the year has more than two digits, only the lower two digits are shown in the result. If the year has fewer than two digits, the number is padded with leading zeros to bring it to two digits.

yyy
represents the year as three digits. If the year has more than three digits, only the three lower digits are shown in the result. If the year has fewer than three digits, the number is padded with leading zeros to make it three digits.

Note that for the Thai Buddhist calendar, where the year can have five digits, this format specifier will display all five digits. 

yyyy
represents the year as four digits. If the year has more than four digits, only the four lower digits are shown in the result. If the year has fewer than four digits, the number is padded with leading zeros to make it four digits.

Note that for the Thai Buddhist calendar, where the year can have five digits, this format specifier will render all five digits. 

yyyyy (plus any number of "y" specifiers)
represents the year as a five-digit number. If the year has more than five digits, only the five lower digits are shown in the result. If the year has fewer than five digits, the number is padded with leading zeros to bring it to five digits.

If there are additional 'y' specifiers, the number is padded with the required number of leading zeros to make up the number of 'y' specifiers. 

z
represents the signed time zone offset of the system time from Greenwich Mean Time (GMT) measured in hours. For example, a computer in the Pacific Standard Time zone would have an offset of "-8".

Offsets are always shown with a leading sign. A plus sign (+) indicates hours are ahead of GMT, and a minus sign (-) indicates hours are behind GMT. The offset range is –12 to +13. One-digit offsets are formatted without leading zeros. The offset is affected by daylight saving time. For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

zz
represents the signed time zone offset of the system time from Greenwich Mean Time (GMT) measured in hours. For example, a computer in the Pacific Standard Time zone would have an offset of "-08".

Offsets are always shown with a leading sign. A plus sign (+) indicates hours are ahead of GMT, and a minus sign (-) indicates hours are behind GMT. The offset range is –12 to +13. One-digit offsets are formatted with leading zeros. The offset is affected by daylight saving time.

zzz, zzz (plus any number of "z" specifiers) 
represents the signed time zone offset of the system time from Greenwich Mean Time (GMT) measured in hours and minutes. For example, a computer in the Pacific Standard Time zone would have an offset of "-08:00".

Offsets are always shown with a leading sign. A plus sign (+) indicates hours are ahead of GMT, and a minus sign (-) indicates hours are behind GMT. The offset range is –12 to +13. One-digit offsets are formatted with leading zeros. The offset is affected by daylight saving time.

:
The time separator defined in the current System.Globalization.DateTimeFormatInfo.TimeSeparator property, used to distinguish hours, minutes, and seconds.

/
The date separator defined in the current System.Globalization.DateTimeFormatInfo.DateSeparator property, used to distinguish between years, months, and days.

"
Quoted string (quote marks). Displays the text value of any string between two quote marks ("). Use the escape character (\) before each quotation mark. 

'
A quoted string (apostrophe). Displays the text value of any string between two apostrophe (') characters.

%c
Indicates the result associated with a custom format specifier when the custom DateTime format string contains only the custom format specifier "c". That is, to use the custom format specifiers "d", "f", "F", "h", "m", "s", "t", "y", "z", " H" or "M", please specify "%d", "%f", "%F", "%h", "%m", "%s", "%t", "%y", " %z", "%H", or "%M". For more information on using individual format specifiers, see Using Individual Custom Format Specifiers.

\c
escape character. Displays the character "c" as text when preceded by an escape character (\). To insert the backslash character itself into the resulting string, use two escape characters ("\\"). 

any other character 
All other characters are copied into the resulting string without affecting formatting.


enumeration format string

format string result 
G or g
Displays the enumeration item as a string value if possible, otherwise displays the integer value of the current instance. If the Flags property is set in the enumeration definition, the string values ​​of each valid item are concatenated and the values ​​are separated by commas. If the Flags property is not set, invalid values ​​are displayed as numeric entries.

F or f
Displays enumeration items as string values, if possible. If the value can be displayed exactly as the sum of the enumeration items (even if the Flags property is not provided), the string values ​​of each valid item are concatenated and the values ​​are separated by commas. If the value cannot be fully determined by the enumeration items, the value is formatted as an integer value.

D or d
Displays enumeration items as integer values ​​in the shortest possible representation.

X or x
Displays enumeration items as hexadecimal values. Represent the value with leading zeros as needed to ensure the value is at least eight digits long

Reprinted: https://www.cnblogs.com/itjeff/p/5775665.html

Guess you like

Origin blog.csdn.net/weixin_42565127/article/details/130872650