SoFunction
Updated on 2025-03-06

Summary of format display methods for DateTime date type in C#

This article summarizes the commonly used DateTime date type format display methods, so that readers can refer to it when using it. The details are as follows:

1. Method for formatting dates during binding:

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

2. Method for formatting dates of data controls such as DataGrid/DataList:

[0].Text = ([0].Text).ToShortDateString();

3. Use String class to convert the date display format:

( "yyyy-MM-dd ",yourDateTime);

4. Use the Convert method to convert the date display format:

("2005-8-23").ToString("yyMMdd",); //Support traditional database

5. Use the ToString method to directly convert the date display format:

("yyyy/MM/dd hh:mm:ss")

or:

("yyyyMMddhhmmss");

6. Show only the year and month

(,"starttime","{0:yyyy-M}")

7. Display all parts of the time, including: year, month, day, hour, minute, and second

&lt;asp:BoundColumn DataField="Cash Time" HeaderText="Cash Time" DataFormatString="{0:yyyy-MM-dd HH24:mm:ss}"&gt;
&lt;/asp:BoundColumn&gt; 

8. Format the time read from the database

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