How to format a time-stamp to show the date only in a grid view

前端 未结 5 2415
南方客
南方客 2021-02-19 20:27

In an aspx page I am binding the labels like this:

  
            
                &l         


        
相关标签:
5条回答
  • 2021-02-19 20:42

    You can specify format strings for the eval statement:

    Eval("date_of_joining", "{0:dd/MM/yyyy}")
    
    0 讨论(0)
  • 2021-02-19 20:43

    You can use the DataFormatString in a bound field the same can be set as below:

    <asp:Label ID="Label8" runat="server" Text='<%# Eval("paid_priviledge_date","{0:d}") %>'/>
    
    0 讨论(0)
  • 2021-02-19 20:51

    Create a FormatDate method in your codebehind, and call that from your gridview.
    http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx
    http://www.csharp-examples.net/string-format-datetime/

    This part will go in your code behind

    private object FormatDate(DateTime input)
    {
        return String.Format("{0:MM/dd/yy}", input);
    }
    

    And this bit will go in your markup

        <asp:TemplateField HeaderText="Date of Joining">
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# FormatDate(Eval("date_of_joining")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Paid Priviledge Date">
            <ItemTemplate>
                <asp:Label ID="Label8" runat="server" 
                    Text='<%# FormatDate(Eval("paid_priviledge_date")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    

    This is what I would call a D.R.Y. approach to the problem. If you ever need to modify the formatting in any way. You can simply edit the code behind method and it will rain down sweet love to all of your markup.

    0 讨论(0)
  • 2021-02-19 20:54
    Text='<%# (Convert.ToDateTime((Eval("date_of_joining")))).ToShortDateString() %>'
    

    This is the simplest way I discovered.

    0 讨论(0)
  • 2021-02-19 20:57

    Use "{0:d}" for short date format. Try

     Text='<%# Eval("paid_priviledge_date","{0:d}") %>'
    

    and

    Text='<%# Eval("date_of_joining", "{0:d}") %>'
    
    0 讨论(0)
提交回复
热议问题