Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

And in the code behind I'm binding the grid view like this :(minimum code is given)

GridView1.DataSource = dt2;
GridView1.DataBind();

But the gridview columns show the date like this :

4/12/2011 12:00:00 AM    
4/4/2011 12:00:00 AM

Please suggest how to remove the time stamp part and to display only the date part.

I know how to do this by formatting using ToString and SubString. But I am not able to do this in gridview.

like image 594
Robin Agrahari Avatar asked Apr 27 '11 15:04

Robin Agrahari


People also ask

How to display only date from DateTime in GridView?

You have to Create a BoundField, set its properties including the property dataformatstring which you can use to set format of the field appear in the GridView, and add it to the GridView control.

How do I change the date format in ASPX?

in ASP.Net. The HTML Markup consists of an ASP.Net Repeater control rendered as an HTML Table with three columns. The DateTime field value is converted to dd/MM/yyyy by passing an additional parameter i.e. {0:dd/MM/yyyy} in order to format and display the DateTime field value in dd/MM/yyyy format.

What is long date format?

The long date option adds the day of the week and writes out the month. So 1/1/19 will display as Tuesday, January 1, 2019. The date will display in the format you choose, no matter how you enter it. If you type "1/1/19" into a cell with the long date format, Excel will change it to the expanded date.


1 Answers

You can specify format strings for the eval statement:

Eval("date_of_joining", "{0:dd/MM/yyyy}")
like image 178
ilivewithian Avatar answered Oct 17 '22 09:10

ilivewithian