[Format processing of date and time in turn].net

Source: Internet
Author: User


One, date and time format processing:

1. Bind timed Format Date method: <asp:boundcolumn datafield= "Time" dataformatstring= "{0:YYYY-MM-DD}" >


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. Convert the date display format directly with the ToString method: DateTime.Now.ToString ("Yyyymmddhhmmss"); DateTime.Now.ToString ("Yyyy/mm/dd hh:mm:ss") 6. Show only the month Databinder.eval_r (Container.DataItem, "StartTime", "{0:yyyy-m} ") 7. Display all parts of the time, including: Month and day minutes <asp:boundcolumn datafield=" collection Time "headertext=" Payment Time "dataformatstring=" {0:YYYY-MM-DD HH24:MM:SS} "> </asp:boundcolumn>

Second, the problems encountered:

(1) in asp.net2.0, the GridView Date column uses dataformatstring= "{0:YYYY-MM-DD}", the output has no change, or is the default "2008-6-19" form.

Workaround: Set the property at the same time: htmlencode= "False"

(2) The format is DateTime.Now.ToString ("Yyyy/mm/dd"), the result of the output is still in the form of 2008-6-19.

Cause: In some languages (such as C #), the "\" character must be preceded by an escape character when it is shared with the ToString method. Workaround:

① enable the second parameter of DateTime.ToString (), ignoring the system time format setting:

DateTime.Now.ToString ("Yyyy/mm/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo); ② uses "yyyy"/' MM '/' dd ' to prevent/M and/d character escapes, ③ uses DateTime.Now.ToString (@ "Yyyy\/mm\/dd"), and is also used to cancel the escape character.

Three. The date type shown is: Yyyy-mm-dd (2011-09-12)

System.DateTime.Now.ToString ("Yyyy-mm-dd");

DateTime dt = DateTime.Now; String str = ""; str = datetime.parse (dt. ToString ()). ToString ("Yyyy-mm-dd"); Label1. Text = str;

Four. Now the format is such as: 2006-09-01 but what I want to achieve is to read out is: 2006-9-1, remove the "0", how to set the system?

System.DateTime.Now.ToString ("yyyy-m-d");

Five. DataFormatString = "{0:d}" display result: 2004-3-29 dataformatstring = "{0:d}" Display results: March 29, 2004

Six. I want to read the date of the day, such as the display: May 24, 2010 I use this method Datetime.today (). Tolongdatestring () displayed on my local computer is May 24, 2010 but after the program is placed on the server (SERVER2003), the first page is displayed in the format: Tuesday, 24, May, 2011 Why is this?

Datetime.today (). ToString ("yyyy mm month DD day");

Seven. Current: Webmaster Academy > website Development >. Net > GridView display format for numbers, currencies, and dates GridView column number, currency, and date display format 2009-8-3 8:53:00 Formal syntax result comment number {0:N2} 1 2.36 number {0:n0} 13 currency {0:C2} $12.36 currency {0:c4} $12.3656 currency "¥{0:n2}" ¥12.36 scientific notation {0:E3} 1.23E+001 percent {0:p} 12. 25% p and P present the same. Date {0:d} November 25, 2006 date {0:d} 2006-11-25 date {0:f} November 25, 2006 10:30 Date {0:f} November 25, 2006 10:30:00 Date {0:s} 2006-11 -26 10:30:00 Time {0:t} 10:30:00

[Format processing of date and time in turn].net

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.