ToString () using Method Rollup (C #) C
Currency
2.5.ToString ("C")
¥2.50
D
Decimal number
25.ToString ("D5")
00025
E
Scientific type
25000.ToString ("E")
2.500000E+005
F
Fixed point
25.ToString ("F2")
25.00
G
Conventional
2.5.ToString ("G")
2.5
Digital
2500000.ToString ("N")
2,500,000.00
X
Hexadecimal
255.ToString ("X")
Ff
Formatcode is an optional format code string. (For more information, search for "format string" view)
Format must be separated from other characters with "{" and "}". If you want to also use curly braces in a format, you can use two consecutive curly braces to represent a brace, that is, "{{" or "}}".
examples of commonly used 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");
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;
Results The value is 123
THIS.TEXTBOX2.TEXT=S2;
Results 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"); Results 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"); Result 12.6%
This.textbox2.text=s; Results The value is 12.6%
(6) DateTime dt =new datetime (2003,5,25);
This.textbox1.text=dt. ToString ("yy"). M.D ");
Results 03.5.25
This.textbox2.text=dt. ToString ("yyyy years M");
Results 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 represents left-aligned, 7-bit
This.textbox1.text=s;
Results i:123, j:123.45
DateTime.ToString () Detailed usage
We often encounter the conversion of time to achieve a different display effect, the default format is: 2006-6-6 14:33:34
What if you want to change into a 200606,06-2006,2006-6-6 or more format?
Here's what you'll use: The DateTime.ToString method (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 detailed usage of the parameter format format is listed here =======================
Format Character Association attribute/description
D ShortDatePattern
D Longdatepattern
F Full Date and time (long date and short time)
F Fulldatetimepattern (long date and long time)
G General (short date and short time)
G General (short date and long time)
M, M Monthdaypattern
R, R Rfc1123pattern
s use local time Sortabledatetimepattern (based on ISO 8601)
T Shorttimepattern
T Longtimepattern
U universalsortabledatetimepattern format for displaying Universal Time
U use the full date and time of the universal Time (long date and long time)
Y, y Yearmonthpattern
The following table lists the patterns that can be merged to construct a custom pattern ========================================
These patterns are case-sensitive; for example, "MM" is recognized, but "MM" is not recognized. If the custom pattern contains white space 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 that are not defined as format characters are copied verbatim.
Format Mode Description:
D One day of the month. The date of one number does not have a leading zero.
One day of DD month. The date of a single number has a leading zero.
The abbreviated name of a day in the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, defined in DayNames.
M-month numbers. A single number of months does not have a leading zero.
MM month number. A one-digit month has a leading zero.
The abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain the era is less than 10, the year is displayed with no leading zeros.
YY does not contain years of the era. If the year that does not contain the era is less than 10, the year with leading zeros is displayed.
YYYY includes the four-digit year of the era.
GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12 Hour system. The number of hours in a single digit does not have a leading zero.
HH 12-hour hour. The number of hours in a single digit has a leading zero.
H 24 hour system. The number of hours in a single digit does not have a leading zero.
HH 24-hour hour. The number of hours in a single digit has a leading zero.
M minutes. The number of minutes in one digit does not have a leading zero.
MM minutes. The number of minutes in a single digit has a leading zero.
s seconds. A number of seconds does not have a leading zero.
SS seconds. The number of seconds in a single digit has a leading zero.
The fractional precision of f seconds is one digit. The remaining digits are truncated.
The fractional precision of FF seconds is two digits. The remaining digits are truncated.
The decimal precision for fff seconds is three digits. The remaining digits are truncated.
The decimal precision for ffff seconds is four digits. The remaining digits are truncated.
The decimal precision for fffff seconds is five digits. The remaining digits are truncated.
The decimal precision for ffffff seconds is six digits. The remaining digits are truncated.
The decimal precision for fffffff seconds is seven digits. The remaining digits are truncated.
t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
The AM/PM indication (if any) that TT is defined in AMDesignator or PMDesignator.
Z Time zone offset ("+" or "-" followed only by hours). The number of hours in a single digit does not have a leading zero. For example, the Pacific Standard Time is "-8".
The ZZ Time zone offset ("+" or "-" follows only the hour). The number of hours in a single digit has a leading zero. For example, the Pacific Standard Time is "-08".
ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes for a single digit has a leading zero. For example, the Pacific Standard Time is " -08:00".
: The default time separator defined in TimeSeparator.
/the default date separator defined in DateSeparator.
% c where C is the format pattern (if used alone). If the format pattern is merged with a literal character or another format pattern, you can omit the "%" character.
\ c where c is any character. Display characters according to the literal meaning. To display the backslash character, use "\".
Only the format 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. The length of the custom pattern is at least two characters;
DateTime.ToString ("D") returns the DateTime value, and "D" is the standard short date pattern.
DateTime.ToString ("%d") returns the day of the month, and "%d" is a custom pattern.
DateTime.ToString ("D") returns the day of the month followed by a blank character, and "D" is a custom pattern.
It is convenient that the above parameters can be arbitrarily combined, and do not go wrong, try more, you will find the time format
To get the time of June 2005 in such a format
You can write this:
Date. ToString ("YYYY year mm month", Datetimeformatinfo.invariantinfo)
So the analogy.
The following is a list of specific date formatting usages in some asp.net: ============================================
1. Format Date method of binding timing:
2. Formatting date methods 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 using the Convert method:
Convert.todatetime ("2005-8-23"). Tostring
("YyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); Support for traditional databases
5. Convert date display format directly with the ToString method:
DateTime.Now.ToString ("Yyyymmddhhmmss");
DateTime.Now.ToString ("Yyyy/mm/dd hh:mm:ss")
6. Show only years
DataBinder.Eval (Container.DataItem, "StartTime", "{0:yyyy-m}")
7. Show all parts of time, including: Date and hour
Dataformatstring= ' {0:yyyy-mm-dd HH24:mm:ss} ' >
Use DateTime.ToString (string format) to output dates in different formats
The DateTime.ToString () function has four overloads. The most commonly used is the one with no parameters. DateTime.ToString (string format) is more powerful and can output dates in different formats. Some of the following are listed for your reference. Some of them are not listed on MSDN.
1. Y represents the year, the note is lowercase y, and the uppercase Y does not represent the year.
2. m represents the month.
3. D indicates the date, noting that D does not represent anything.
4. h or H for hours, h for 12-hour system, h for 24-hour system.
5. m represents minutes.
6. s represents seconds. Note that s doesn't mean anything.
Format
Output
Example
Years
Y
7
String yy = DateTime.Now.ToString ("y-mm")
Yy= "7-05"
Yy
07
String yy = DateTime.Now.ToString ("yy-mm")
Yy= "07-05"
yyy or more Y
1984
String yy = DateTime.Now.ToString ("yyyy");
Yy= "2007"
Month
M
5.
String mon = DateTime.Parse ("1984-05-09") ToString ("Yyyy-m")
Mon = "1984-5"
Mm
05.
String mon = DateTime.Parse ("1984-05-09") ToString ("MM")
Mon = "05"
MMM
If it is a Chinese version of the operating system, it will be output: May.
If it is an English operating system, enter the first three-letter shorthand for the month: May
String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")
English version of the operating system: April
Chinese version of the operating system: July
Mmmm or more M
If it is a Chinese version of the operating system, it will be output: May.
If it is an English operating system, enter full write for the month
String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")
English version of the operating system: July
Chinese version of the operating system: July
Date or week
D
9
String dd= datetime.parse ("1984-05-09") ToString ("D")
Dd= "9"
Dd
09
String dd= datetime.parse ("1984-05-09") ToString ("DD")
dd= "09"
Ddd
If it is a Chinese version of the operating system, it will output weeks, such as Wednesday ...
In the case of an English operating system, the abbreviation for the week is output:
Wed
string dd = DateTime.Parse ("2006-07-01"). ToString ("ddd")
English version of the operating system: Wed
Chinese version of the operating system: Wednesday
dddd or more D
If it is a Chinese version of the operating system, it will output weeks, such as Wednesday ...
If it is an English operating system, the output week:
Wednesday
string dd = DateTime.Parse ("2006-07-01"). ToString ("dddd")
English version of the operating system: Wednesday
Chinese version of the operating system: Wednesday
Hours
H
Hour Range: 1-12
string hh = DateTime.Now.ToString ("h");
HH = 8
HH or more H
Hour Range: 1-12
string hh = DateTime.Now.ToString ("hh");
HH = 08
H
Hour Range: 0-23
string hh = DateTime.Now.ToString ("yyyy-h");
HH = 2006-8
HH or more H
Hour Range: 0-23
string hh = DateTime.Now.ToString ("yyyy-hh");
HH = 2006-08
string hh = Datetime.pare ("2006-7-4 18:00:00"). ToString ("yyyy-hh");
HH = 2006-18
Minutes
M
6
string mm = DateTime.Now.ToString ("yyyy-mm-dd-m");
MM = "2006-07-01-6";
mm or more M
06
string mm = DateTime.Now.ToString ("yyyy-mm-dd-mm");
MM = "2006-07-01-06";
Seconds
6
string mm = DateTime.Now.ToString ("Yyyy-mm-dd-s");
MM = "2006-07-01-6";
SS or more S
06
string mm = DateTime.Now.ToString ("Yyyy-mm-dd-ss");
MM = "2006-07-01-06";
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.