First, format the value of numerical results can be formatted by the string.format method or through the console.write method, and the latter method calls String.Format. Use the format string to specify the format. The table below contains the supported standard format string. The format string is in the form of AXX, where A is "format specifier", and XX is "accurate specifier". Format Description Control Application to the value of the value, and the accuracy indicator controls the significant bits or decimal digits of the formatted output.
For more information on standard and custom formatted strings, see
Format overview
. For more information on String.Format methods, see
String.Format method
.
Character Description Example Output C or C Currency 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); 00025E or E Scientific Console.write ("{0: E}", 250000); 2.500000E 005F or F fixed point console.write ("{0: f2} ", 25); console.write (" {0: F0} ", 25); 25.00 25g or g General console.write (" {0: g} ", 2.5); 2.5N or N digital console.write (" {0: N} ", 2500000); 2,500,000.00X or X hex console.write (" {0: x} ", 250); console.write (" {0: x} ", 0xfff); fa ffff
Second, the date format
Standard DateTime format string
If the format string contains only a single format specifier listed in the table below, they are interpreted as a standard format specifier. If the specified format character is a single character and is not included in the table below, it is anomalous. If the format string is longer than a single character longer (even if the extra characters are blank), the format string is interpreted as a custom format string. Note that the mode generated by these format specifiers is affected by the settings in the Area Options control panel. Computers with different regions or different dates and time settings will display different modes.
The time and date separator displayed by the format string are made from current regional
DateTimeFormat
Attribute association
Dateseparator
with
TimeseParetor
Character definition. However, if
InvariantCulture
The characters associated with DateSeparator and TimeSeparetor characters are not changed with current regional changes. The following table describes the standard format strings that format the DateTime object.
Format Description Name Description D Short date mode Display is associated with the current thread
DatetimeFormatinfo.shortdatePattern
The mode defined by the property or the mode defined by the specified format. D Long Date Mode Display is associated with the current thread
DateTimeFormatinfo.longdatePattern
The mode defined by the property or the mode defined by the specified format. T short time mode display is associated with the current thread
DateTimeFormatinfo.shortTimePattern
The mode defined by the property or the mode defined by the specified format. T length mode display is associated with the current thread
DateTimeFormatinfo.longTimePattern
The mode defined by the property or the mode defined by the specified format. f Complete Date / Time Mode (short time) Displays a combination of long-term and short time modes, separated by spaces. F Complete Date / Time Mode (Long Time) Display is associated with the current thread
DateTimeFormatinfo.FullDateTimePattern attribute definitions or modes defined by the specified format. g Regular Date / Time Mode (short time) Displays a combination of short-term and short time mode, separated by spaces. G Regular Date / Time Mode (Long Time) Displays a combination of short date and long mode, separated by spaces. M or M Moon mode display is associated with current threads
DateTimeFormatinfo.monthdaypattern
The mode defined by the property or the mode defined by the specified format. R or RRFC1123 mode display is associated with the current thread
DateTimeFormatinfo.rfc1123pattern
The mode defined by the property or the mode defined by the specified format. This is the standard defined, and attribute is read-only; therefore, it is always the same regardless of what the area or the format provider is provided. The property references the CultureInfo.invariantCulture property and follows the custom mode "DDD, DD MMMM YYYY HH: MM: SS G / MT". Note that "M" in "GMT" requires an escaping, so it is not interpreted. S can be sorted by date / time mode; in line with ISO 8601 display is associated with current threads
DateTimeFormatinfo.sortableDateTimePattern
The mode defined by the property or the mode defined by the specified format. The property references the CultureInfo.invariantCulture property, the format complies with custom mode "YYYY-MM-DDTHH: MM: SS". u GMSLO date / time mode display is associated with the current thread
DateTimeFormatinfo.UniversalsalsalSortableDateTimePattern
The mode defined by the property or the mode defined by the specified format. Because it is a defined standard, and attribute is read-only, the mode is always the same regardless of the regional or format provider. Format Follow Custom Mode "YYYY-MM-DD HH: MM: SSZ". U GMSLO date / time mode display is associated with the current thread
DateTimeFormatinfo.FullDateTimePattern
The mode defined by the property or the mode defined by the specified format. Please note that the display time is a versatile time, not local time. Y or Y Yun Moon Mode Display is associated with the current thread
DateTimeFormatinfo.YearMonthpattern
The mode defined by the property or the mode defined by the specified format. Any other single character unknown specifier
The table below shows a list of format specifier examples that apply to any value of DateTime.now for public date and time information. Different regional settings are given to explain the impact of the current regionality. This is usually changed in the following: Use the Date / Time control panel in Microsoft Windows to pass your own DateTimeFormatinfo object as the format provider, or transfer the CultureInfo object setting to different regions. Note that for "R" and "S" format, change zone does not affect the output. This table is a quick guide to how standard date and time specifiers affect formatting. Please refer to this table to explain the code example section of these setsis.
Format Description Current Regional Output DEN-US4 / 10 / 2001DEN-NZ10 / 04 / 2001DDE-DE10.04.2001DEN-USTESDAY, APRIL 10, 2001TEN-US3: 51: 24 PMTES-ES15: 51: 24FEN-USTESDAY, APRIL 10 , 2001 3:51 PMFFR-FRMARDI 10 AVRIL 2001 15: 51REN-USTUE, 10 APR 2001 15:51:24 GMTREN-SGTUE, 10 APR 2001 15:51:24 Gmtsen-US2001-04-10T15: 51: 24SPT-BR2001 -04-10t15: 51: 24uen-US2001-04-10 15: 51: 24ZUSV-FI2001-04-10 15: 51: 24Zmen-USAPRIL 10MMS-MY10 APRILYEN-USAPRIL, 2001YAF-ZAAPRIL 2001LEN-UZ Unrecognized format description The format is abnormal. The following code example illustrates how to use a custom format string using the DateTime object.
[Visual Basic]
DIM DT as datetime = datetime.now
DIM 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 Pass.
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 Pass.
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 DateTime format string
Sometimes there is more control of the formatting of the DateTime object. As a replacement method of standard DateTime format, you can construct your own DateTime formatting mode using custom DateTime format string. In fact, the standard format is derived from these custom formats.
The table below shows the custom format specifier and describes the values they generate. The output of these strings is affected by the current regionality and settings in the Area Options control panel.
Format Description Description D shows the current date of the month, represents 1 and 31 in a number of 1 to 31. If there is only one digit (1-9), it is displayed as a number. Note that if the "D" format specifier is used separately, there is no other custom format string, it is interpreted as a standard short date mode format specifier. If the "D" format specifier is passed along with other custom format specifiers, it is interpreted as a custom format specifier. DD shows the current date of the month, represents 1 and 31, including 1 and 31. If the date is only one digit (1-9), it is formatted as a leading 0 (01-09). DDD Displays the daily partial partition name of the specified DateTime object. If a specific valid format provider is not provided (a non-empty object with the IFORMATPROVIDER with the expected property), use the AbbreviatedDayNames property of the DateTimeFormat object and the current region associated with the current thread. Otherwise, using the AbbreviatedDayNames property from the specified format provider. DDDD (plus any number of additional "D" characters) Displays a full name of the day part of the specified datetime object. If a specific valid format provider is not provided (non-empty objects with the IFORMATPROVIDER with the expected attribute), use the DAYNAMES attribute of the DateTimeFormat object and the current regionality associated with the current thread. Otherwise, use the DayNames property from the specified format provider. f shows seconds represented by a digit. Note that if the "F" format specifier is used separately, there is no other custom format string, it is interpreted as a complete (long dates short time) format specifier. If the "F" format specifier is passed along with other custom format specifiers, it is interpreted as a custom format specifier. ff displays seconds represented by two digits. FFF displays seconds represented by three digits. FFFF shows seconds represented by four digits. Ffffff shows seconds represented by five digits. Ffffff shows seconds represented by six digits. fffffff is displayed in seconds representing seven digits. G or GG (external additional number of additional "g" characters) displays the age part (such as A.D.) of the specified DateTime object. If a specific valid format provider is not provided (non-empty objects with the IFORMATPROVIDER with the expected attribute), the era is determined by the calendar associated with the datetimeformat object and its current regionality associated with the current thread. Note that if the "G" format specifier is used separately, there is no other custom format string, it is interpreted as a standard conventional format specifier. If the "G" format specifier is passed with other custom format specifiers, it is interpreted as a custom format specifier. h Displays the hour portion of the specified datetime object in a number in this range of 1 to 12. This hourly portion indicates the number of times after midnight (shown as 12) or noon (also shown as 12). If this format is used alone, it is not possible to distinguish when it is noon or later at noon. If this hour is a single number (1-9), it is displayed as a single number. No rounding occurs when you display hours. For example, the DateTime returns 5 when 5:43. HH, HH (external number of additional "H" characters) displays the hour portion of the specified datetime object in a range of 1 to 12. This hourly portion indicates the number of times after midnight (shown as 12) or noon (also shown as 12). If this format is used alone, it is not possible to distinguish when it is noon or later at noon.
If the hour is a single number (1-9), it is formatted to have 0 (01-09). H Displays the hour portion of the specified datetime object in a range of 0 to 23. This hourly portion indicates the number of times after midnight (shown as 0). If this 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 part of the specified datetime object in a range of 0 to 23. This hourly portion indicates the number of times after midnight (shown as 0). If the hour is a single number (0-9), it is formatted to have 0 (01-09) in front. m Displays a minute portion of the specified datetime object in a range of 0 to 59. The minute portion indicates the number of interstitials passed from 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, there is no other custom format string, it is interpreted as a standard monthly mode format specifier. If the "M" format specifier is passed with other custom format specifiers, it is interpreted as a custom format specifier. MM, mm (plus any number of additional "M" characters) displays a minimum part of the specified DateTime object in a range of 0 to 59. The minute portion indicates the number of interstitials passed from the last hour. If the minute is a digit (0-9), it is formatted as a leading 0 (01-09). M Displays the current month, representing 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, there is no other custom format string, it is interpreted as a standard monthly mode format specifier. If the "M" format specifier is passed with other custom format specifiers, it is interpreted as a custom format specifier. MM displays the current month, represents 1 and 12, including 1 and 12. If the month is a digit (1-9), it is formatted as a leading 0 (01-09). MMM Displays the Monthly Some Site Repeck Name of the DateTime object. If a specific valid format provider is not provided (non-empty objects with the IFORMATPROVIDER with the expected attribute), use the AbbreviatedMontHnames property of the DateTimeFormat object and the current region of the current thread associated with the current thread. Otherwise, using the AbbreviatedMonthnames property from the specified format provider. MMMM displays a full name of the monthly part of the specified datetime object. If a specific valid format provider is not provided (non-empty objects with the IFORMATPROVIDER with the expected attribute), use the MONTHNAMES attribute of the DateTimeFormat object and the current regionality associated with the current thread. Otherwise, using the monthNames property from the specified format provider. s Displays the second portion of the specified datetime object in a range of 0 to 59. The second part indicates the number of times after the last minute. If the second is a digit (0-9), it is only displayed as a digit. Note that if the "S" format specifier is used separately, there is no other custom format string, it is interpreted as a standard silence date / time mode format specifier. If the "S" format specifier is passed from other custom format descriptions, it is interpreted as a custom format specifier. SS, SS (plus any number of additional "S" characters) displays the second part of the specified datetime object in a range of 0 to 59. The second part indicates the number of times after the last minute. If seconds are a digit (0-9), it is formatted to bring a leading 0 (01-09).
t Displays the first character of the specified datetime object A.M./p.m. If a specific valid format provider is not provided (non-empty objects with the IFORMATPROVIDER with the expected attribute), use the AMDESIGNATOR of the DateTimeFormat object.
(or
PMDesignator
) Properties and current regionalities associated with current threads. Otherwise, use the AMDesignator (or PMDesignator) attribute from the specified iFormatProvider. Use AMDESIGNATOR if the total overall number of times passed by the specified DateTime is less than 12. Otherwise, use PMDesignator. Note that if the "T" format specifier is used separately, there is no other custom format string, it is interpreted as a standard long mode format specifier. If the "T" format specifier is passed along with other custom format specifiers, it is interpreted as a custom format specifier. TT, TT (plus any number of additional "T" characters) Displays the A.M./p.m of the specified datetime object. If a specific valid format provider is not provided (non-empty objects with IFORMATPROVIDER with expected properties), use the AMDESIGNATOR attribute of the DateTimeFormat object and the current region associated with the current thread. Otherwise, use the AMDesignator (or PMDesignator) attribute from the specified iFormatProvider. Use AMDESIGNATOR if the total overall number of times passed by the specified DateTime is less than 12. Otherwise, use PMDesignator. Y Y will specify the year part of the DateTime object as a number of up to 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 number of bits of up to two digits in the year section of the specified DateTime object. Ignore the first two digits of the year. If the year is a digit (1-9), it is formatted as a leading 0 (01-09). YYYY displays the age part of the specified DateTime object (including century). If the year length is less than four digits, then zero is added to the front to make the displayed year length to four. Z Only the time zone offset of the system current time zone is displayed at the entire number of hours. The offset is always displayed as a leading or trailing symbol (zero display " 0") indicating the hour number than Greenwich Time ( ) or late Greenwich Time (-). The value range is -12 to 13. If the offset is one digit (0-9), it is displayed as a one with the appropriate preamble symbol. The setting of the time zone is specified as x or -x, where x is an offset of the relative GMT in hours. The displayed offset is affected by the summer time. ZZ is only displayed at the time of the unit to display the time zone offset of the system current time zone. The offset is always displayed as a leading or trailing symbol (zero display " 00") indicates that the number of hours ( ) or late Greenwich time (-) earlier. 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 with an appropriate preamble symbol. The setting of the time zone is specified as x or -x, where x is an offset of the relative GMT in hours. The displayed offset is affected by the summer time. ZZZ, ZZZ (plus any number of additional "z" characters) Displays the time zone offset of the system current time zone in hours and minutes. The offset is always displayed as a leading or trailing symbol (zero display as " 00: 00"), indicating the hour and minute of Greenwich time ( ) or late Greenwich 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 with an appropriate preamble symbol. The setting of the time zone is specified as x or -x, where x is an offset of the relative GMT in hours.
The displayed offset is affected by the summer time. : Time separator. / Date separator. "String with quotation marks. The text value of any string between the two quotes after the exemplary character (/) is displayed. 'The string with quotation marks. Show the text between two"' "characters Value.% C where C is the standard format character, the standard format mode associated with the format character ./c where C is any character, the escape character is displayed as text. In this context, the escape character cannot be used Create a escape sequence (such as "/ n" means a wrap). Any other character other characters are written directly to the output string directly. When the custom mode is passed to DateTime.toString, the mode must be at least two characters. If only Passing "D", the public language runtime interprets it as a standard format specifere, because all individual format specifiers are interpreted as a standard format specifier. If a single "H" is passed, the reason is not There is a standard "H" format specifier. To format only a single custom format, add a space in front of the setup. 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 displays the current time). Different regional and time zone settings are given in the example to explain the impact of changes in regionality. You can change the current regionality by the following method: Change the value in the "Date / Time" control panel in Microsoft Windows, pass your own DateTimeFormatinfo object, or transfer the CultureInfo object setting to different regions. This table is to explain how the custom date and time specifier affect the formatted quick guide. Please refer to this table to explain the code example section of these setsis.
Format specifier output the current culture time zone d, Men-USGMT12, 4d, Mes-MXGMT12, 4d MMMMen-USGMT12 Aprild MMMMes-MXGMT12 Abrildddd MMMM yy ggen-USGMTThursday April 01 ADdddd MMMM yy gges-MXGMTJueves Abril 01 DCh, m: sen -USGMT6, 13: 12HH, MM: SSEN-USGMT06, 13: 12HH-MM-SS-Tten-USGMT06-13-12-AMHH: mm, g / mt z en-usgmt05: 13 GMT 0HH: mm, g / MT Z EN-USGMT 10: 0005: 13 GMT 10HH: mm, g / mt zzzen-usgmt05: 13 GMT 00: 00HH: mm, g / mt zzzen-usgmt -9: 0005: 13 GMT-09: 00
Note that in some languages (such as C #), "/" "When shared with the toString method, it must have an escape character.
The following code example explains how to create a custom formatted string from the DateTime object. This example assumes that the current regionality is US 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 Culture, MyString Has The value: "2000 a.d.".
[C #]
DateTime mydate = new datetime (2000, 1, 1, 0, 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 CULTURE, MYSTRING HAS The value: "2000 a.d.".