C DateTime date type format display method summary


This paper summarizes the commonly used DateTime date type formatting display methods, for the convenience of readers in the use of reference 1. The details are as follows:

1. Date formatting method during binding:

<ASP:BOUNDCOLUMN DATAFIELD= "JoinTime " DATAFORMATSTRING= "{0:yyyy-MM-dd} " >
<ITEMSTYLE WIDTH= "18% " > </ITEMSTYLE >
</ASP:BOUNDCOLUMN >

2. Data controls such as DataGrid/DataList, etc.

e.Item.Cell[0].Text = Convert.ToDateTime(e.Item.Cell[0].Text).ToShortDateString();

3. Use String class to convert date display format:

String.Format( "yyyy-MM-dd ",yourDateTime);

4. Use Convert method to convert date display format:

Convert.ToDateTime("2005-8-23").ToString("yyMMdd",System.Globalization.DateTimeFormatInfo.InvariantInfo); // Support traditional database

5. Directly convert date display format with ToString method:

DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")

Or:

DateTime.Now.ToString("yyyyMMddhhmmss");

6. Only show the year and year

DataBinder.Eval(Container.DataItem,"starttime","{0:yyyy-M}")

7. Display all parts of the time, including: minutes and seconds of year, month and day

<asp:BoundColumn DataField=" Payment time " HeaderText=" Payment time " DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}">
</asp:BoundColumn>

8. Format the time to read from the database

Convert.ToDateTime(dr["MT_ENDate"]).ToShortDateString();