Convert data datetime to dd / MM / yyyy

8

I have a GridView and I'm using TemplateField to display the date that is currently in the format 'string yyyy-MM-dd 00: 00: 00'. >

I need to show this date only with dd/MM/yyyy . I have already tried {0:dd/MM/yyyy} and it does not work. Can anyone help me?

    
asked by anonymous 25.05.2015 / 22:27

1 answer

4

The following code correctly formats the date, please confirm that your code is equivalent:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "data_inicio", "{0:dd/MM/yyyy}")%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

After receiving some more information per comment, I learned that the property used in the grid column is not a DateTime type. To appear in the desired format, you need to convert to DateTime and then format again as string :

<ItemTemplate>
    <%# Convert.ToDateTime(Eval("Data")).ToString("dd/MM/yyyy")%>
</ItemTemplate>

But I suggest you do not convert the property to string and send DateTime to Grid.

    
25.05.2015 / 23:48