Format Date and display in label asp.net RRS feed

  • Question

  • User-2093561334 posted

    I have one textbox and it's text mode is Date.<br>
    How to format date from that text and separate dd, mm, yy and show in different labels
    Sunday, May 5, 2019 9:07 AM

All replies

  • User283571144 posted

    Hi  Naninani,
    According to your question, I'm not sure if you want to show the date in textbox through label or if you want to change the date format in textbox.
    If you want to show the date in textbox through label, you could refer to below codes:
    <asp:TextBox ID="TextBox2" runat="server" TextMode="Date" AutoPostBack="true" OnTextChanged="TextBox2_TextChanged"></asp:TextBox><br />
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>/
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>/
    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    code behind
    protected void TextBox2_TextChanged(object sender, EventArgs e)
      Label1.Text = string.Format("{0:MM}", Convert.ToDateTime(TextBox2.Text));
      Label2.Text = string.Format("{0:dd}", Convert.ToDateTime(TextBox2.Text));
      Label3.Text = string.Format("{0:yy}", Convert.ToDateTime(TextBox2.Text));

    If you want to change the date format in textbox, I recommend that you use the default format of textbox. By setting the format in the code behind, you can set any date format you want. You can refer to the following code.

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
      TextBox1.Text = string.Format("{0:yyyy-MM-dd}", Calendar1.SelectedDate);


    Best Regards,


    Monday, May 6, 2019 3:17 AM