Analysis on Displaying Time and Date Format in GridView

  • 2021-07-22 09:28:30
  • OfStack

The following are the basic date and time formats commonly used in GridView

形式

语法

结果

注释

数字

{0:N2}

12.36

 

数字

{0:N0}

13

 

货币

{0:c2}

$12.36

 

货币

{0:c4}

$12.3656

 

货币

"¥{0:N2}"

¥12.36

 

科学计数法

{0:E3}

1.23E+001

 

百分数

{0:P}

12.25%

P and p present the same.

日期

{0:D}

2006年11月25日

 

日期

{0:d}

2006-11-25

 

日期

{0:f}

2006年11月25日 10:30

 

日期

{0:F}

2006年11月25日 10:30:00

 

日期

{0:s}

2006-11-25 10:30:00

 

时间

{0:T}

10:30:00

 

Pay attention to the following aspects when operating in GridView (take {0: d} as an example)

1. If the column to bind is simply of type boundfiled (as follows)

< asp: BoundField DataField= "sj" HeaderText= "Time" ReadOnly= "True" DataFormatString= "{0: d}"/ >

The format displayed is still 2006-11-25 0:00:00 to achieve the desired effect

2. The column to bind must be the template column as follows: In order to see the effect we want to see: 2006-11-25


<asp:TemplateField HeaderText=" Time ">

  <EditItemTemplate>

     <asp:Label ID="Label1" runat="server" Text='<%# Eval("sj", "{0:d}") %>'></asp:Label>

</EditItemTemplate>

<ItemTemplate>

     <asp:Label ID="Label2" runat="server" Text='<%# Bind("sj", "{0:d}") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

Related articles: