Custom DateTime format in GridView TemplateField columns in ASP.Net

Last Reply 13 days ago By dharmendr

Posted 13 days ago

hello

i have label inside list view i want to apply date format to it

<asp:Label ID="Label2" runat="server" Text='<%# Eval("date", "{0:dd/M/yyyy}") %>' ></asp:Label>

currently it returns 

  • Aug 05, 2019

i want it like

  • Monday, Aug 05, 2019

 

so with day

Posted 13 days ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("date", "{0:dddd, MMM dd, yyyy}") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[] { 
                        new System.Data.DataColumn("Id", typeof(int)),
                        new System.Data.DataColumn("date",typeof(DateTime)) });
        dt.Rows.Add(1, new DateTime(2019, 08, 05));
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As System.Data.DataTable = New System.Data.DataTable()
        dt.Columns.AddRange(New System.Data.DataColumn() {
                            New System.Data.DataColumn("Id", GetType(Integer)),
                            New System.Data.DataColumn("date", GetType(DateTime))})
        dt.Rows.Add(1, New DateTime(2019, 8, 5))
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

Output

IdDate
1 Monday, Aug 05, 2019