The six methods of time formatting in Asp.net are summarized in detail

  • 2020-05-24 05:31:01
  • OfStack

1. Format date method when data control is bound:
 
<asp:BoundColumn DataField="AddTime" HeaderText=" Add the time " DataFormatString="{0:yyyy-MM-dd HH:mm}></asp:BoundColumn> 
<asp:BoundField DataField="AddTime" HeaderText=" Add the time " DataFormatString="{0:yyyy-MM-dd}" /> 

2. When using DataBinder.Eval for data binding:
 
DataBinder.Eval(Container.DataItem,"AddTime","{0:yyyy-MM-dd}") 

3. Directly convert the date display format with ToString method:
 
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 

4. Convert date display format with String class:
 
String.Format("{0:yyyy-MM-dd}", DateTime.Now) 

5. Convert the date display format with the DateTime.Parse method
 
DateTime.Parse("2012-2-22").ToString("yyyy-MM-dd") 

6. Convert the date display format with the Convert.ToDateTime method:
 
Convert.ToDateTime("2012-12-21").ToString("yyyy-MM-dd") 

Standard DateTime format specifier
http://msdn.microsoft.com/zh-cn/library/az4se3k1(v=vs.80).aspx
Customize the DateTime format specifier
http://msdn.microsoft.com/zh-cn/library/8kb3ddd4(v=vs.80).aspx

Related articles: