ASP. NET ToString () format rollup

Source: Internet
Author: User
Tags iso 8601 string format truncated

C

Currency

2.5.ToString("C")

¥2.50

D

Decimal number

ToString("D5")

00025

E

Scientific type

25000.ToString("E")

2.500000E+005

F

Fixed point

ToString("F2")

25.00

G

Conventional

2.5.ToString("G")

2.5

N

Digital

2500000.ToString("N")

2,500,000.00

X

Hexadecimal

255.ToString("X")

Ff

Formatcode is an optional format code string. (For more information, please search for "formatted string" view)
You must use "{" and "}" to separate the format from other characters. If you want to use curly braces exactly in the format, you can use two consecutive curly braces to denote a curly brace, which is: "{{" 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");
Results 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"); Results 12345.68
This.textbox2.text=i.ToString("F6");
Results 12345.678900
(4) Double i=12345.6789;
This.textbox1.text=i.ToString("n"); Result 12,345.68
This.textbox2.text=i.ToString("N4"); Result 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; Results 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");
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 = left-justified, 7-bit
This.textbox1.text=s;
Results i:123, j:123.45
DateTime.ToString()Usage Explanation
We often encounter the conversion of time to achieve different display results, 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?
This is going to be used:datetime.tostringThe 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 parameter format format detailed usage is listed here
=======================
Format Character Association properties/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 the 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 combined 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 quotation marks, the output string page will also contain these characters. Characters that are not defined as part of the format pattern or that are not defined as format characters are copied verbatim.
Format Pattern Description:
A day in the D month. One-digit date has no leading zeros.
DD a day of the month. A one-digit date has a leading zero.
The abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, as defined in DayNames.
M-month number. One-digit month has no leading zeros.
MM month number. One-digit month has a leading zero.
Abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, as defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
YY does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
The 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 hour system. One-digit hours do not have leading zeros.
HH 12-hour hour. One-digit hours have leading zeros.
H 24 hour hour system. One-digit hours do not have leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.
M minutes. A single-digit number of minutes does not have a leading zero.
MM minutes. A single-digit number of minutes has a leading zero.
s seconds. The number of seconds in a single digit does not have a leading zero.
SS seconds. The number of seconds of one digit has a leading zero.
The fractional precision of the F-second is one digit. The remaining digits are truncated.
The fractional precision of the FF seconds is two bits. The remaining digits are truncated.
The fractional precision of FFF seconds is three bits. The remaining digits are truncated.
The fractional precision of ffff seconds is four bits. The remaining digits are truncated.
The fractional precision of fffff seconds is five bits. The remaining digits are truncated.
The fractional precision of ffffff seconds is six bits. The remaining digits are truncated.
The fractional precision of fffffff seconds is seven bits. 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 indicated by the TT in AMDesignator or PMDesignator (if present).
The Z Time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8".
ZZ Time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08".
The ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00".
: The default time delimiter defined in TimeSeparator.
/The default date delimiter defined in DateSeparator.
% c where C is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns.
\ c where c is any character. Displays the characters as they are originally defined. To display a backslash character, use "\ \".
Only the format patterns listed in the second table above can be used to create custom patterns, and the standard format characters listed in the first table cannot be used to create custom patterns. The custom pattern is at least two characters long;
Datetime.ToString("D") returns a DateTime value; "D" is a standard short date pattern.
Datetime.ToString("%d") returns the day of the month; "%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.
More convenient is that the above parameters can be arbitrarily combined, and will not be wrong, try more, will definitely find the time format you want
To get the time in this format for June 2005
It can be written like this:
Date.ToString("YYYY years mm month", Datetimeformatinfo.invariantinfo)
So the analogy.

The following is a list of some of the specific date formatting uses in asp:
============================================
1. Bind timed Format Date method:



2. Data controls such as Datagrid/datalist format Date method:
E.item.cell[0]. Text = Convert.todatetime (e.item.cell[0]. Text). ToShortDateString ();
3. Use the string class to convert the date display format:
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); Support for traditional databases
5. Direct useToStringMethod Convert date display format:
DateTime.Now.ToString("Yyyymmddhhmmss");
DateTime.Now.ToString("Yyyy/mm/dd hh:mm:ss")
6. Show only Month
DataBinder.Eval (Container.DataItem, "StartTime", "{0:yyyy-m}")
7. Display all parts of the time, including: Month Day minutes and seconds
Dataformatstring= ' {0:yyyy-mm-dd HH24:mm:ss} ' >
Use DateTime.ToString(string format) to output dates in different formats


Datetime.ToString() function has four overloads. Generally used much is the one without parameters. As everyone knows, 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, note is lowercase y, and uppercase y does not represent the year.
2. m represents the month.
3. D represents the date, noting that D does not mean anything.
4. h or H represents hours, H is 12 hours, H is 24 hours.
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 the Chinese version of the operating system, it will be output: May.
If it is an English operating system, enter the first three letters of the month: May

String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")
English version of operating system: Jul
Chinese version of the operating system: July

Mmmm or more M

If it is the Chinese version of the operating system, it will be output: May.
If it is an English operating system, enter the full write of the month

String mon = DateTime.Parse ("2006-07-01"). ToString ("MMM")
English version of operating system: July
Chinese version of the operating system: July

Date or day of the 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 the Chinese version of the operating system, it will output the week, such as Wednesday.
If it is an English operating system, the output Week is abbreviated:
Wed

string dd = DateTime.Parse ("2006-07-01"). ToString ("DDD")
English version of operating system: Wed
Chinese version of the operating system: Wednesday

dddd or more D

If it is the Chinese version of the operating system, it will output the week, 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 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

S

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";

ASP. NET ToString () format Rollup

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.