C | currency | 2.5.ToString("C") | ¥2.50 | D | Decimal numbers | 25.ToString("D5") | 00025 | E | Scientific type | 25000.ToString("E") | 2.500000E+005 | F | Fixed point | 25.ToString("F2") | 25.00 | G | Regular | 2.5.ToString("G") | 2.5 | N | numbers | 2500000.ToString("N") | 2,500,000.00 | X | Hex system | 255.ToString("X") | FF |
formatCode is an optional formatting code string. (For details, please search for "format string" to view) The formatting must be separated from the rest of the characters with "{" and "}". If you happen to use braces in the format as well, you can use two consecutive braces to represent a curly brace, i.e.: "{{" or "}}". Examples of common formats: (1) int i=12345; this.textBox1.Text=i.ToString(); Result 12345 (this refers to the current object, or an instance of the current class) this.textBox2.Text=i.ToString("d8"); The result 00012345 (2) int i=123; double j=123.45; string s1=string. Format("the value is {0,7:d}",i); string s2=string. Format("the value is {0,7:f3}",j); this.textBox1.Text=s1 ; The result is 123 this.textBox2.Text=s2; Result the value is 123.450 (3)double i=12345.6789; this.textBox1.Text=i.ToString("f2"); Result 12345.68 this.textBox2.Text=i.ToString("f6"); Result 12345.678900 (4)double i=12345.6789; this.textBox1.Text=i.ToString("n"); Result 12,345.68 this.textBox2.Text=i.ToString(“n4”); Results 12,345.6789 (5)double i=0.126; string s=string. Format("the value is {0:p}",i); this.textBox1.Text=i.ToString("p"); Results 12.6% this.textBox2.Text=s; Result the value is 12.6% (6) DateTime dt =new DateTime(2003,5,25); this.textBox1.Text=dt.ToString("yy. M.d"); Result 03.5.25 this.textBox2.Text=dt.ToString("yyyy year M month"); Result May 2003 Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss") "2005/12/22 22:22:22" (7) int i=123; double j=123.45; string s=string. Format("i:{0,-7},j:{1,7}",i,j); -7 indicates left alignment, occupying 7 digits this.textBox1.Text=s ; Result i: 123, J: 123.45
DateTime.ToString()Detailed explanation of usage We often encounter the conversion of time to achieve different display effects, the default format is: 2006-6-6 14:33:34 What if I want to change to 200606, 06-2006, 2006-6-6 or more? Here we will use:DateTime.ToStringmethod(String, IFormatProvider) Example:
using System; using System.Globalization; String format="D"; DateTime date=DataTime.Now; Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo)); Result output Thursday, June 16, 2006
The parameter format is listed here for detailed usage ======================= Format Characters Associated attributes/descriptions d ShortDatePattern D LongDatePattern f Full date and time (long date and short time) F FullDateTimePattern (long date and long time) g Regular (short date and short time) G Regular (short date and long time) m、M MonthDayPattern r、R RFC1123Pattern s uses SortableDateTimePattern for local time (based on ISO 8601) t ShortTimePattern T LongTimePattern u UniversalSortableDateTimePattern is used to display the format of the universal time U Full date and time (long date and long time) using common time y、Y YearMonthPattern The following table lists the patterns that can be merged to construct custom patterns ======================================== These patterns are case-sensitive; For example, identify "MM" but not "mm". If the custom pattern contains whitespace characters or characters enclosed in single quotes, the output string page will also contain those characters. Characters that are not defined as part of a format pattern or are not defined as format characters are copied in their original meaning. Format Mode Description : d One day in the month. Single-digit dates do not have leading zeros. dd one day in the month. A single-digit date has a leading zero. ddd The abbreviated name of a day in the week, defined in AbbreviatedDayNames. dddd The full name of a day in the week, defined in DayNames. M month numbers. Single-digit months do not have leading zeros. MM month numbers. Single-digit months have a leading zero. The abbreviated name of the MMM month, defined in AbbreviatedMonthNames. MMMM The full name of the month, defined in MonthNames. y does not contain the year of the epoch. If a year that does not contain an epoch is less than 10, the year without a leading zero is displayed. yy does not contain the year of the epoch. If the year that does not contain an epoch is less than 10, the year with the leading zero is displayed. yyyy includes four-digit years of the epoch. gg period or epoch. If the date you want to format doesn't have an associated period or epoch string, ignore the pattern. h 12-hour hours. There are no leading zeros in single-digit hours. HH hours in a 12-hour system. The number of hours in the single digits has leading zeros. H hours in the 24-hour system. There are no leading zeros in single-digit hours. HH 24-hour hours. The number of hours in the single digits has leading zeros. m minutes. There are no leading zeros in single-digit minutes. mm min. The number of minutes in the single digits has a leading zero. s seconds. There are no leading zeros in single-digit seconds. ss seconds. The number of single-digit seconds has a leading zero. The decimal precision of f seconds is one digit. The rest of the numbers are truncated. The decimal precision of ff seconds is two digits. The rest of the numbers are truncated. The decimal precision of fff seconds is three digits. The rest of the numbers are truncated. The decimal precision of ffff seconds is four digits. The rest of the numbers are truncated. fffff seconds have a decimal precision of five digits. The rest of the numbers are truncated. The decimal precision of ffffff seconds is six digits. The rest of the numbers are truncated. fffffff seconds have a decimal precision of seven digits. The rest of the numbers are truncated. t The first character of the AM/PM indicator defined in the AMDesignator or PMDesignator, if present. tt AM/PM indicator defined in the AMDesignator or PMDesignator, if present. z time zone offset ("+" or "-" followed by hours only). There are no leading zeros in single-digit hours. For example, Pacific Standard Time is "-8". zz time zone offset ("+" or "-" followed by hours only). The number of hours in the single digits has leading zeros. For example, Pacific Standard Time is "-08". zzz full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes in single digits has leading zeros. For example, Pacific Standard Time is "-08:00". : The default time separator defined in TimeSeparator.
/ 在 DateSeparator 中定义的默认日期分隔符。 % c where c is the format pattern (if used alone). If the format pattern is merged with the original meaning character or other formatting patterns, the "%" character can be omitted. \c where c is an arbitrary character. Characters are displayed in their original meaning. To display the backslash character, use "\\". Only the formatting patterns listed in the second table above can be used to create custom patterns; The standard format characters listed in the first table cannot be used to create custom patterns. Custom patterns must be at least two characters long; For example DateTime.ToString( "d") returns the DateTime value; "d" is the standard short date pattern. DateTime.ToString( "%d") Return to a day in the month; "%d" is the custom mode. DateTime.ToString( "d ") returns a day in the month followed by a spaced character; d" is a custom mode. It is more convenient that the above parameters can be combined at will and will not be wrong, try more, and you will definitely find the time format you want If you want to get the time in this format in 2005.06 It can be written like this: date.ToString("yyyy year MM month", DateTimeFormatInfo.InvariantInfo) And so on.
Here are some specific date formatting usages in Asp.net: ============================================ 1. Format date method when binding:
2. Formatting date method for data controls such as DataGrid/DataList: e.Item.Cell[0]. Text = Convert.ToDateTime(e.Item.Cell[0]. Text). ToShortDateString(); 3. Convert date display format with String class: String.Format( "yyyy-MM-dd ",yourDateTime); 4. Convert date display format with Convert method: Convert.ToDateTime("2005-8-23").ToString ("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo); Supports traditional databases 5. Use directlyToStringMethod Convert Date Display Format: DateTime.Now.ToString("yyyyMMddhhmmss"); DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") 6. Only the year and month are displayed DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}") 7. Display all parts of the time, including: year, month, day, hour, minute, and second DataFormatString='{0:yyyy-MM-dd HH24:mm:ss}'> Use DateTime.ToString(string format) to output dates in different formats
|