Change date format Example in ASP.NET using C#

Change date format Example in ASP.NET using C#


Posted in : ASP.Net Posted on : December 1, 2010 at 6:18 PM Comments : [ 0 ]

In this article we will introduce how can we change date format in ASP.NET using C#.

Change Date Format in ASP.NET using C#

In this example you can see how can I change the format of Date. There are three separately DropDownList for day, month and year. When user selects the day, month and year from DropDownList and click submit Button. The result will be displayed in label with different different format.

ChangeDateFormat.aspx (Design Page):

ChangeDateFormat.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="ChangeDateFormat.aspx.cs" Inherits="ChangeDateFormat" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color: Green">
Change Date Format in ASP.NET 4, C#</h2>
<br />
<strong>Your Birth Date: </strong>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="submitButton" runat="server" onclick="submitButton_Click" 
Text="Submit" Width="86px" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
<br />
<br />
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
<br />
<br />
<asp:Label ID="Label4" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
<br />
<br />
<asp:Label ID="Label5" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
<br />
<br />
<asp:Label ID="Label6" runat="server" Font-Bold="True" Font-Names="Verdana" 
Font-Size="Small" ForeColor="#000099" />
</div>
</asp:Content>

ChangeDateFormat.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ChangeDateFormat : System.Web.UI.Page
{
int i;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Insert(0, new ListItem("---DD---", "---DD---"));
DropDownList2.Items.Insert(0, new ListItem("---MM---", "---MM---"));
DropDownList3.Items.Insert(0, new ListItem("--YYYY--", "--YYYY--"));
for (i = 1; i < 32; i++)
{
DropDownList1.Items.Add(i.ToString());
}
for (i = 1; i < 13; i++)
{
DropDownList2.Items.Add(i.ToString());
}
for (i = 1950; i < 2011; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
}
protected void submitButton_Click(object sender, EventArgs e)
{
Label1.Text = "Date format(DD/MM/YYYY) is : " + DropDownList1.Text.ToString() 
+ "/" + DropDownList2.Text.ToString() + "/" + DropDownList3.Text.ToString();
Label2.Text = "Date format(DD-MM-YYYY) is : " + DropDownList1.Text.ToString() 
+ "-" + DropDownList2.Text.ToString() + "-" + DropDownList3.Text.ToString();
Label3.Text = "Date format(MM/DD/YYYY) is : " + DropDownList2.Text.ToString() 
+ "/" + DropDownList1.Text.ToString() + "/" + DropDownList3.Text.ToString();
Label4.Text = "Date format(MM-DD-YYYY) is : " + DropDownList2.Text.ToString() 
+ "-" + DropDownList1.Text.ToString() + "-" + DropDownList3.Text.ToString();
Label5.Text = "Date format(YYYY/MM/DD) is : " + DropDownList3.Text.ToString() 
+ "/" + DropDownList2.Text.ToString() + "/" + DropDownList1.Text.ToString();
Label6.Text = "Date format(YYYY-MM-DD) is : " + DropDownList3.Text.ToString() 
+ "-" + DropDownList2.Text.ToString() + "-" + DropDownList1.Text.ToString();
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics