String in Microsoft. NET Framework format

Source: Internet
Author: User
Tags iso 8601 month name time and date

1. Use {0 :?} Format

You can use the string. Format method or the console. Write method to format numeric results. The latter method calls string. format. Use a format string to specify the format. The following table contains supported standard format strings. The format string is in the form of axx, where A is the format specifier, and XX is the precision specifier ". The format specifiers are used to control the formatting type of numeric values, while the precision specifiers control the number of valid digits or decimal places for formatting output.

For more information about standard and custom formatting strings, see formatting overview. For more information about the string. Format method, see the string. Format method.

Character Description sample output
Console. Write ("{0: c}", 2.5 );
Console. Write ("{0: c}",-2.5 );
$2.50
($2.50)
D or D decimal console. Write ("{0: D5}", 25); 00025
E or E scientific console. Write ("{0: e}", 250000); 2.500000e + 005
F or F Fixed Point Console. Write ("{0: F2}", 25 );
Console. Write ("{0: F0}", 25 );
25.00
25
G or G conventional console. Write ("{0: g}", 2.5); 2.5
N or N digital console. Write ("{0: n}", 2500000); 2,500,000.00
X or X hexadecimal console. Write ("{0: x}", 250 );
Console. Write ("{0: x}", 0 xFFFF );
Fa
FFFF

Ii. Date formatting

String in standard datetime format
If the format strings only contain a single format specifier listed in the following table, they are interpreted as standard format specifiers. If the specified format character is a single character and does not contain the following table, an exception is thrown. If the format string is longer than a single character (even if the extra characters are blank), the format string is interpreted as a custom format string. Note that the modes produced by these format specifiers are affected by the settings in the "region options" control panel. Computers with different cultures or different date and time settings will display different modes.

The time and date delimiter displayed by the format string are defined by the dateseparator and timeseparator characters associated with the datetimeformat attribute of the current culture. However, if invariantculture is referenced by the "r", "S", and "U" specifiers, the characters associated with the dateseparator and timeseparator characters do not change with the current culture. The following table describes the standard format strings used to format datetime objects.

Format description
D. The short date mode displays the mode defined by the datetimeformatinfo. shortdatepattern attribute associated with the current thread or provided by the specified format Program The defined mode.
D. The long date mode displays the mode defined by the datetimeformatinfo. longdatepattern attribute associated with the current thread or the mode defined by the provider in the specified format.
T short-time mode displays the mode defined by the datetimeformatinfo. effectimepattern attribute associated with the current thread or the mode defined by the provider in the specified format.
T long time mode displays the mode defined by the datetimeformatinfo. longtimepattern attribute associated with the current thread or the mode defined by the provider in the specified format.
F. The combination of long date and short time modes is displayed in full date/time mode (short time), separated by spaces.
F full date/time mode (long time) displays the mode defined by the datetimeformatinfo. fulldatetimepattern attribute associated with the current thread or the mode defined by the provider in the specified format.
G conventional date/time mode (Short Time) shows the combination of short date and short time mode, separated by spaces.
G regular date/time mode (long time) shows the combination of short date and long time mode, separated by spaces.
The m or M month/day mode displays the mode defined by the datetimeformatinfo. monthdaypattern attribute associated with the current thread or the mode defined by the provider in the specified format.
The R or R rfc1123 mode displays the mode defined by the datetimeformatinfo. rfc1123pattern attribute associated with the current thread or the mode defined by the provider in the specified format. This is the defined standard and the attribute is read-only. Therefore, it is always the same regardless of the culture or the provided format provider. The attribute references the cultureinfo. invariantculture attribute and follows the custom mode "DDD, DD mmmm yyyy hh: mm: SS g \ MT ". Note that "M" in "GMT" requires an escape character, so it is not interpreted.
S: sorted date/time mode. ISO 8601 is used to display the mode defined by the datetimeformatinfo. sortabledatetimepattern attribute associated with the current thread or the mode defined by the provider in the specified format. The attribute references the cultureinfo. invariantculture attribute. The format complies with the custom mode "yyyy-mm-ddthh: mm: SS ".
U common sortable date/time mode displays the mode defined by the datetimeformatinfo. universalsortabledatetimepattern attribute associated with the current thread or the mode defined by the provider in the specified format. Because it is a defined standard and its attributes are read-only, the pattern is always the same no matter what the regional or format provider is. The format follows the custom mode "yyyy-mm-dd hh: mm: SSZ ".
The universal sorting date/time mode of U displays the mode defined by the datetimeformatinfo. fulldatetimepattern attribute associated with the current thread or the mode defined by the provider in the specified format. Note that the displayed time is the general time, not the local time.
Y or y year-on-year mode displays the mode defined by the datetimeformatinfo. yearmonthpattern attribute associated with the current thread or the mode defined by the provider in the specified format.
Unknown specifiers for any other single character

The following table shows a list of format specifiers. These examples are applied to any value of datetime. Now that exposes the current date and time information. In this example, different regional settings are given to illustrate the impact of changing the current regional. This is usually changed in the following ways: use the "date/time" control panel in Microsoft Windows to pass your own datetimeformatinfo object as a format provider, or pass the settings of the cultureinfo object to different cultures. Note that for the "R" and "S" formats, changing the culture does not affect the output. This table is a quick guide to how standard Date and Time specifiers affect formatting. See the descriptions in this table.CodeSample section.

Format description: output of the current region
D en-US 4/10/2001
D en-NZ 10/04/2001
D de-de 10.04.2001
D en-US Tuesday, 10000l 10,200 1
T en-US 3:51:24
T es-es 15:51:24
F en-US Tuesday, 10000l 10,200 1 pm
F fr-fr Mardi 10 avril 2001
R en-US Tue, 10 Apr 2001 15:51:24 GMT
R ZH-SG Tue, 10 Apr 2001 15:51:24 GMT
S en-US 2001-04-10t15: 51: 24
S Pt-Br 2001-04-10t15: 51: 24
U en-US 2001-04-10 15: 51: 24z
U sv-fi 2001-04-10 15: 51: 24z
M en-US limit L 10
M Ms-my 10 then l
Y en-US limit L, 2001
Y af-za 450l 2001
L format specifiers that are not recognized by en-uz; format exceptions are thrown.

The following code example illustrates how to use a custom string for a datetime object.

[Visual Basic] Dim dT as datetime = datetime. nowdim DFI as datetimeformatinfo = new datetimeformatinfo () dim CI as cultureinfo = new cultureinfo ("de-de") 'make up a new custom datetime pattern, for demonstration. DFI. monthdaypattern = "MM-MMMM, DDD-dddd" 'Use the datetimeformat from the culture associated' with the current thread. console. writeline (DT. tostring ("D") console. writeline (DT. tostring ("M") 'Use the datetimeformat from the specific culture passed. console. writeline (DT. tostring ("D", CI) 'use the settings from the datetimeformatinfo object passed. console. writeline (DT. tostring ("M", DFI) 'reset the current thread to a different culture. thread. currentthread. currentculture = new cultureinfo ("FR-be") console. writeline (DT. tostring ("D "))
[C #] datetime dt = datetime. now; datetimeformatinfo DFi = new datetimeformatinfo (); cultureinfo CI = new cultureinfo ("de-de"); // make up a new custom datetime pattern, for demonstration. DFI. monthdaypattern = "MM-MMMM, DDD-dddd"; // use the datetimeformat from the culture associated // with the current thread. console. writeline (DT. tostring ("D"); console. writeline (DT. tostring ("M"); // use the datetimeformat from the specific culture passed. console. writeline (DT. tostring ("D", CI); // use the settings from the datetimeformatinfo object passed. console. writeline (DT. tostring ("M", DFI); // reset the current thread to a different culture. thread. currentthread. currentculture = new cultureinfo ("FR-be"); console. writeline (DT. tostring ("D "));
Custom string in datetime format
Sometimes you need more control over the formatting of datetime objects. As a replacement method of the standard datetime format, you can use a custom datetime format string to construct your own datetime format. In fact, the standard format is derived from these custom format specifiers.

The following table shows the custom format specifiers and describes their generated values. The output of these strings is affected by the current culture and settings in the "region options" control panel.

Format description
D shows the current date of the month, represented by a number between 1 and 31, including 1 and 31. If the date has only one digit (1-9), it is displayed as a digit.
Note that if the "D" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard short date format specifier. If the "D" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
Dd displays the current date of the month, represented by a number between 1 and 31, including 1 and 31. If the date has only one digit (1-9), format it with a leading 0 (01-09 ).
Ddd displays the abbreviated name of the day part of the specified datetime object. If no specific valid format provider is provided (a non-empty object that implements the iformatprovider with the expected attributes), The abbreviateddaynames attribute of the datetimeformat object and the current culture associated with the current thread are used. Otherwise, use the abbreviateddaynames attribute from the specified format provider.
Dddd (plus any number of additional "D" characters) displays the full name of the day part of the specified datetime object. If no specific valid format provider is provided (a non-empty object that implements the iformatprovider with the expected attributes), The daynames attribute of the datetimeformat object and the current culture associated with the current thread are used. Otherwise, use the daynames attribute from the specified format provider.
F indicates the second in one digit.
Note that if the "f" format specifier is used separately and there are no other custom format strings, it is interpreted as a complete (long date + short time) format specifier. If the "f" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
FF indicates the second in two digits.
Fff displays the seconds in three digits.
FFFF indicates the second in four digits.
Fffff displays the seconds in five digits.
Ffffff displays the seconds in six digits.
Fffffff displays the seconds in seven digits.
G or Gg (plus any number of additional "G" characters) displays the time section of the specified datetime object (for example, A. D .). If no specific valid format provider is provided (a non-empty object that implements the iformatprovider with the expected attributes ), the date is determined by the calendar associated with the datetimeformat object and the current culture associated with the current thread.
Note that if the "G" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard regular format specifier. If the "G" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
H displays the hour part of the specified datetime object with a number in the range of 1 to 12. This hour indicates the whole hour that has elapsed since midnight (displayed as 12) or noon (also displayed as 12. If this format is used separately, it cannot be different from the time before or after noon for an hour. If the hour is a single number (1-9), it is displayed as a single number. It indicates that no round occurs in the hour. For example, if datetime is, 5 is returned.
HH, HH (plus any number of additional "H" characters) displays the hour portion of the specified datetime object with a number ranging from 1 to 12. This hour indicates the whole hour that has elapsed since midnight (displayed as 12) or noon (also displayed as 12. If this format is used separately, it cannot be different from the time before or after noon for an hour. If the hour is a single number (1-9), format it with 0 (01-09) in front ).
H is a number ranging from 0 to 23 to display the hour part of the specified datetime object. This hour indicates the whole hour that has elapsed since midnight (displayed as 0. If the hour is a single number (0-9), it is displayed as a single number.
HH, HH (plus any number of additional "H" characters) displays the hour portion of the specified datetime object with a number ranging from 0 to 23. This hour indicates the whole hour that has elapsed since midnight (displayed as 0. If the hour is a single number (0-9), format it with 0 (01-09) in front ).
M is a number ranging from 0 to 59 to display the minute portion of the specified datetime object. This minute indicates the number of minutes that have elapsed since the last hour. If the minute is a digit (0-9), it is displayed as a digit.
Note that if the "M" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard month-day format specifier. If the "M" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
Mm, mm (plus any number of additional "M" characters) displays the minute portion of the specified datetime object with a number ranging from 0 to 59. This minute indicates the number of minutes that have elapsed since the last hour. If the minute is a digit (0-9), format it with a leading 0 (01-09 ).
M shows the current month, represented by a number between 1 and 12, including 1 and 12. If the month is a digit (1-9), it is displayed as a digit.
Note that if the "M" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard month-day format specifier. If the "M" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
MM displays the current month, represented by a number between 1 and 12, including 1 and 12. If the month is a digit (1-9), format it with a leading 0 (01-09 ).
Mmm displays the abbreviated month name of the specified datetime object. If no specific valid format provider is provided (a non-empty object that implements the iformatprovider with the expected attributes), The abbreviatedmonthnames attribute of the datetimeformat object and the current culture associated with the current thread are used. Otherwise, use the abbreviatedmonthnames attribute from the specified format provider.
Mmmm displays the full name of the month part of the specified datetime object. If no specific valid format provider is provided (a non-empty object that implements the iformatprovider with the expected attributes), The monthnames attribute of the datetimeformat object and the current culture associated with the current thread are used. Otherwise, use the monthnames attribute from the specified format provider.
S displays the second part of the specified datetime object with a number ranging from 0 to 59. The second part indicates the whole number of seconds since the last minute. If the second is a digit (0-9), it is displayed as only one digit.
Note that if the "S" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard sortable date/time mode format specifier. If the "S" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
SS, SS (plus any number of additional "S" characters) display the second part of the specified datetime object with a number ranging from 0 to 59. The second part indicates the whole number of seconds since the last minute. If the second is a digit (0-9), format it with a leading 0 (01-09 ).
T displays the first character of the specified datetime object a. m./P. M. Indicator. If no specific valid format provider is provided (non-empty objects that implement iformatprovider with expected properties), use the amdesignator (or pmdesignator) of the datetimeformat object) attribute and the current region associated with the current thread. Otherwise, use the amdesignator (or pmdesignator) Attribute from the specified iformatprovider. If the total number of hours for the specified datetime is less than 12, amdesignator is used. Otherwise, use pmdesignator.
Note that if the "T" format specifier is used separately and there are no other custom format strings, it is interpreted as a standard long-time format specifier. If the "T" format specifier is passed together with other custom format specifiers, it is interpreted as a custom format specifier.
TT and TT (plus any number of additional "T" characters) display the. m./P. M. Indicator of the specified datetime object. If no specific valid format provider is provided (non-empty objects that implement iformatprovider with expected properties), use the amdesignator (or pmdesignator) of the datetimeformat object) attribute and the current region associated with the current thread. Otherwise, use the amdesignator (or pmdesignator) Attribute from the specified iformatprovider. If the total number of hours for the specified datetime is less than 12, amdesignator is used. Otherwise, use pmdesignator.
Y displays the year of the specified datetime object as a number with a maximum of two digits. Ignore the first two digits of the year. If the year is a digit (1-9), it is displayed as a digit.
YY displays the year of the specified datetime object as a number with a maximum of two digits. Ignore the first two digits of the year. If the year is a digit (1-9), format it with a leading 0 (01-09 ).
Yyyy displays the year (including Century) of the specified datetime object ). If the length of a year is less than four digits, add zero to the beginning to increase the length of the year to four digits.
Z only displays the time zone offset of the current time zone of the system in units of the hour. The total offset is displayed with a leading or trailing sign (0 is displayed as "+ 0"), indicating the hours earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-12 to + 13. If the offset is a single digit (0-9), it is displayed as a single digit with a proper leading symbol. This time zone is set to + X or-X, where X is the offset relative to GMT in hours. The displayed offset is affected by the daylight saving time.
ZZ only displays the time zone offset of the current time zone of the system in units of the hour. The total offset is displayed with a leading or trailing sign (0 is displayed as "+ 00"), indicating the hours earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-12 to + 13. If the offset is a single number (0-9), format it to the front with 0 (01-09) and a proper leading symbol. This time zone is set to + X or-X, where X is the offset relative to GMT in hours. The displayed offset is affected by the daylight saving time.
Zzz and ZZZ (plus any number of additional "Z" characters) display the time zone offset of the system's current time zone in units of hours and minutes. The offset is always displayed with a leading or trailing sign (0 is displayed as "+ 00:00"), indicating the hours and minutes earlier than Greenwich Mean Time (+) or later than Greenwich Mean Time. The value range is-12 to + 13. If the offset is a single number (0-9), format it to the front with 0 (01-09) and a proper leading symbol. This time zone is set to + X or-X, where X is the offset relative to GMT in hours. The displayed offset is affected by the daylight saving time.
: Time separator.
/Date separator.
"String with quotation marks. Displays the text value of any string between two quotation marks after the escape character.
'String with quotation marks. Displays the text value of any string between two "'" characters.
% C, where C is a standard character, displays the Standard Format mode associated with the format character.
\ C contains any character, and the Escape Character displays the next character as text. In this context, the escape character cannot be used to create an escape sequence (for example, "\ n" indicates a line break ).
Other characters can be directly written to the output string as text.

When you pass the custom mode to datetime. tostring, the mode must be at least two characters long. If only "D" is passed, the common language runtime interprets it as a standard format specifier because all single format specifiers are interpreted as standard format specifiers. If a single "H" is passed, an exception is thrown because there is no standard "H" format specifier. To format only a single custom format, add a space before or after the description. For example, the format string "H" is interpreted as a custom format string.

The following table shows an example of using any value datetime. Now (this value shows the current time. The example provides different settings for the region and time zone to illustrate the impact of changing the region. You can change the current culture by changing the value in the date/time control panel of Microsoft Windows, passing your own datetimeformatinfo object, or passing the settings of the cultureinfo object to different cultures. This table is a quick guide to how custom Date and Time specifiers affect formatting. See the sample code that explains these specifiers below this table.

Format description: output of the current regional Time Zone
D, m en-us gmt 12, 4
D, m es-mx GMT 12, 4
D mmmm en-us gmt 12 w.l
D mmmm es-mx GMT 12 Abril
Dddd mmmm YY Gg en-us gmt Thursday limit L 01 A.D.
Dddd mmmm YY Gg es-mx GMT jueves Abril 01 DC
H, M: s en-us gmt 6, 13: 12
HH, MM: SS en-us gmt 06,13: 12
Hh-mm-SS-TT en-us gmt 06-13-12-AM
Hh: Mm, G \ Mt Z en-us gmt 05:13 GMT + 0
Hh: Mm, G \ Mt Z en-us gmt + 10: 00 05:13 GMT + 10
Hh: Mm, G \ Mt ZZZ en-us gmt 05:13 GMT + 00:00
Hh: Mm, G \ Mt ZZZ en-us gmt-

Note that in some languages (such as C #), the "\" character must be preceded by an escape character when used with the tostring method.

The following code example illustrates how to create a custom formatted string from a datetime object. This example assumes that the current region is American English (En-US ).

[Visual Basic]
dim mydate as new datetime (2000, 1, 1, 0, 0, 0)
dim mystring as string = mydate. tostring ("dddd-D-mmmm")
// 'in the U. s. english culture, mystring has the value: '"Saturday-1-January ".
mystring = mydate. tostring ("yyyy gg")
// 'in the U. s. english cultures, mystring has the value: "2000. d. ".
[C #]
datetime mydate = new datetime (2000, 1, 1, 0, 0);
string mystring = mydate. tostring ("dddd-D-mmmm");
// in the U. s. english culture, mystring has the value: "Saturday-1-January ".
mystring = mydate. tostring ("yyyy gg");
// in the U. s. english cultures, mystring has the value: "2000. d. ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.