How can you show the date in a gridview as a date only instead of a datetime?

前端 未结 10 1424
粉色の甜心
粉色の甜心 2021-02-04 01:22

I am pulling in date values from a sql server database using a gridview and and the date gets converted from

12/12/2009 to 12/12/2009 12:

相关标签:
10条回答
  • 2021-02-04 01:36

    set the dataformatstring value to "{0:d}"

    Ex:

    <asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
    </asp:BoundField>
    
    0 讨论(0)
  • 2021-02-04 01:40

    Try the below code:

        <asp:BoundField DataField="my_date" HeaderText="Date" 
                                ReadOnly="True" SortExpression="my_date" 
                                DataFormatString="{0:d}" />
    

    In the above mentioned code, my_date is the date column of the sqlserver table. The DataFormatString="{0:d}" is the main portion of this code to resolve the particular issue of yours.

    0 讨论(0)
  • 2021-02-04 01:41

    Within

    asp:Label runat="server" Text='<%# Eval("DateAndTime") %>'

    Try adding "{0:M-dd-yyyy}"

    asp:Label runat="server" Text='<%# Eval("DateAndTime", "{0:M-dd-yyyy}") %>'

    0 讨论(0)
  • 2021-02-04 01:44

    You can use the ToString() method with a mask:

    ToString("MM/dd/yyyy");
    

    UPDATE: Just realized it would be easier in your case to do this in the grid view template

    <asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />
    
    0 讨论(0)
  • 2021-02-04 01:45

    You can use a DataAnnotations attribute and a DynamicField control; then you don't have to do the same formatting every time you want to format that field. There is an example showing how to do this here: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-8

    0 讨论(0)
  • 2021-02-04 01:46

    You can also use .ToShortDateString() on the DateTime Object if you are already manipulating the date in the RowDataBound

    0 讨论(0)
提交回复
热议问题