Reddy Reddy - 3 months ago 15
Vb.net Question

asp.net DataGrid Convert YYYYMMDD to MM/DD/YYYY format

Input data is 20160902 (YYYYMMDD)

I'm trying to display this as MM/dd/yyyy in ASP.NET Data Grid and tried to use DataFormatString="{0:MM/dd/yyyy}" which didn't work.

Can some one help me with this?

Answer

I assume that your data field is not a date. It may be a string or an integer. You could make sure that your data is retrieved as a date and format it with DataFormatString:

<asp:BoundColumn DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />

If, for some reason, that option is not available, you can show your data field in a TemplateColumn of the DataGrid (replace "MyDate" in the example below by the actual name of your data field):

<asp:TemplateColumn>
    <ItemTemplate>
        <asp:Literal runat="server" Text='<%# DateTime.ParseExact(Eval("MyDate").ToString(), "yyyyMMdd", null).ToString("MM/dd/yyyy") %>' />
    </ItemTemplate>
</asp:TemplateColumn>
Comments