UncleCoder.com

UncleCoder.com

Free programming examples and instructions

Change date format asp.net c#

Demo and Code for how to change date format in Asp.net C#

by Athil


Posted on 14 Jul 2017 Category: Asp.net Views: 573

Edited on 17 Jul 2017



DEMO

Hi, Here I am going to show how to change date format in asp.net c#.

Sample Code change date format

              DateTime d = new DateTime(2017,1,31);
              TextBox1.Text = d.ToString("dd-MMM-yyyy");

In the above code I created an object for class DateTime d and assign a date for object d then I assign to the text box and change the date format.

Demo Code for change date format

on Client Side

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title>Demo for change date format is Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Demo for change date format in Asp.net </h1>

        <h3> Please click any of the button to see date in different format in text box </h3>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />

        <asp:Button ID="Button1" runat="server" Text="dd-MMM-yyyy" OnClick="Button1_Click" />
    </div>
        <p>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="dd-MM-yyyy" />
        </p>
        <p>
            <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="yyyy/MM/dd" />
        </p>
        <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="d-MMM-yyyy hh:mm tt" />
</form>
</body>
</html>

On Server Side

 

protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            TextBox1.Text = d.ToString("dd-MMM-yyyy");

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            TextBox1.Text = d.ToString("dd-MM-yyyy");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            DateTime d = DateTime.Now;
            TextBox1.Text = d.ToString("yyy/MM/dd");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
             DateTime d = DateTime.Now;
            TextBox1.Text = d.ToString("d-MMM-yyyy hh:mm tt");
            //where tt denotes am/pm
            
        }

 



Leave a Comment:


Advertisement