C#datetime.tostring formatting time strings and numeric types into strings

Source: Internet
Author: User
Tags truncated

We often encounter the conversion of time to achieve different display results, the default format is: 2006-6-6 14:33:34, if you want to change to 200606,06-2006,2006-6-6 or more format what to do? Here's how to use: datetime.tostring.

First, datetime.tostring format mode

The format parameter in datetime.tostring (string format) is listed below. These patterns are case-sensitive, for example, "MM" is recognized, but "MM" is not recognized.


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).


Ii. Examples of application

C # codeCopy
     April 24, 2007
     System.DateTime.Now.ToString ("D");
     2007-4-24
     System.DateTime.Now.ToString ("D");
     April 24, 2007 16:30:15
     System.DateTime.Now.ToString ("F");
     April 24, 2007 16:30
     System.DateTime.Now.ToString ("F");
     2007-4-24 16:30:15
     System.DateTime.Now.ToString ("G");
     2007-4-24 16:30
     System.DateTime.Now.ToString ("G");
     16:30:15
     System.DateTime.Now.ToString ("T");
     16:30
     System.DateTime.Now.ToString ("T");
     April 24, 2007 8:30:15
     System.DateTime.Now.ToString ("U");
     2007-04-24 16:30:15z
     System.DateTime.Now.ToString ("U");
     2007-04-24 15:52:19
     System.DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss:ffff");
     April 2007 24 15:56 48 seconds
     System.DateTime.Now.ToString ("yyyy mm month dd hh when mm minutes ss seconds");
     Tuesday, April 24 2007
     System.DateTime.Now.ToString ("dddd, MMMM dd yyyy");
     II, April 24 ' 07
     This. TextBox2.Text = System.DateTime.Now.ToString ("ddd, MMM d \ \ ' yy");
     Tuesday, April 24
     System.DateTime.Now.ToString ("dddd, MMMM dd");
     4-07
     System.DateTime.Now.ToString ("M/yy");
     24-04-07
     System.DateTime.Now.ToString ("Dd-mm-yy");

DateTime.Now other functions converted to strings

C # codeCopy
           String str = "";
            str + = "toshortdatestring:" + DateTime.Now.ToShortDateString () + "<br>";
            str + = "toshorttimestring:" + DateTime.Now.ToShortTimeString () + "<br>";
            str + = "tolongdatestring" + DateTime.Now.ToLongDateString () + "<br>";
            str + = "tolongtimestring" + DateTime.Now.ToLongTimeString () + "<br>";
            str + = "ToString:" + DateTime.Now.ToString () + "<br>";
            Response.Write (str);
            Output Result:
            Toshortdatestring:2006-2-13
            Toshorttimestring:12:51
            Tolongdatestring:  February 13, 2006
            Tolongtimestring:  12:51:17
            ToString:          2006-2-13 12:51:17


First, the numeric type is converted to a string

C

Currency

2.5.ToString ("C")

¥2.50

D

Decimal number

25.ToString ("D5")

25.ToString ("D3")

00025

025

E

Scientific type

25000.ToString ("E")

2.500000E+005

F

Fixed point

25.ToString ("F2")

25.ToString ("F1")

25.00

25.0

G

Conventional

2.5.ToString ("G")

2.5

N

Digital

2500000.ToString ("N")

2500000.ToString ("N0")

2,500,000.00

2,500,000

X

Hexadecimal

255.ToString ("X")

Ff

# # #转载于: http://www.studyofnet.com/news/1349.html

C#datetime.tostring formatting time strings and numeric types into strings

Related Article

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.