1.11M Members

Want Date Format in dd/mm/yyyy in ASP.Net With C#.

 
0
 

Dear Friend,

I am new in ASP.Net with c#.

I want date in a dd/mm/yyyy format with validation in TextBox . Is anybody guide me how shall ?

As I got date with dd/mm/yyyy but it will also accept 32/02/2005. Though Feb has less 30 days.

Also when I compare two textbox it will not evalute properly. eg I want TextBox2 date greater then TextBox1. So I had used
CompareValidator, & select type="date" but it will not evalute.

Pl guide me.


Thanking You,

 
0
 

How about something like this

private void DateSubmit_Click(object sender, System.EventArgs e)
		{
			DateTime date1, date2;
			bool date1OK, date2OK;
			date1 = new DateTime(1,1,1);
			date2 = new DateTime(1,1,1);
			try
			{
				date1 = Convert.ToDateTime(Date1.Text);
				date1OK=true;
			}
			catch
			{
				date1OK = false;
			}
			try
			{
				date2 = Convert.ToDateTime(Date2.Text);
				date2OK=true;
			}
			catch
			{
				date2OK = false;
			}
			if(date1OK && date2OK)
			{
				if(date1.CompareTo(date2) > -1)lblResult.Text = "Date2 must be after Date1";
				else
				{
					//do whatever here
				}
			}
			else lblResult.Text = "Invalid date format. Please check your input in the Date1 and Date2 fields";
		}
 
0
 

You can use the asp:CustomValidator control of ASP.NET to achieve the mentioned functionality.

The HTML would look like:
<input type="text" id="TextBox1" runat="server" name="TextBox1">

<asp:CustomValidator Runat="server" ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="Invalid DateTime Format"
ID="Customvalidator1" ClientValidationFunction="ValidateTextBox1" OnServerValidate="CustomValidaorMethod"></asp:CustomValidator>

and on server side (code behind of the page) you would have a public method like :

public void CustomValidaorMethod(object source, ServerValidateEventArgs args)
{
string valuePassed = string.Empty;
if(args.Value != null)
valuePassed = args.Value;
DateTime dt = DateTime.MinValue;
args.IsValid = true;
try
{
IFormatProvider format = new CultureInfo( "en-US");


//write custom code to validate date time or just parse it
using DateTime.Parse


dt = DateTime.Parse(valuePassed,format,DateTimeStyles.None);
}
catch
{
args.IsValid = false;
}
}

This would validate the Textbox content, also when you want to compare, you can use asp:CompareValidator to do it:
it would look like

<asp:CompareValidator ControlToCompare="TextBox2" ControlToValidate="TextBox1" EnableClientScript="true"             Runat="server" ID="CompareValidator" Type='Date' Operator="Equal" ErrorMessage="Dates do not match" Enabled="True"></asp:CompareValidator>

One thing that must be kept in mind when using Asp.Net validators that post back methods should always check for Page.IsValid value that tells the page if validators returned true or false and then proceed further.

There is one more exciting thing you can do: you can enable client script function and write a client script function to do it using a script language say javascript.

(This i leave for you to figure out)

Thanks,
Ashwani

 
0
 

Try from Control Panel with Regional setting,chage type of calendar(julian|gregorian) :surprised

How about something like this

private void DateSubmit_Click(object sender, System.EventArgs e)
		{
			DateTime date1, date2;
			bool date1OK, date2OK;
			date1 = new DateTime(1,1,1);
			date2 = new DateTime(1,1,1);
			try
			{
				date1 = Convert.ToDateTime(Date1.Text);
				date1OK=true;
			}
			catch
			{
				date1OK = false;
			}
			try
			{
				date2 = Convert.ToDateTime(Date2.Text);
				date2OK=true;
			}
			catch
			{
				date2OK = false;
			}
			if(date1OK && date2OK)
			{
				if(date1.CompareTo(date2) > -1)lblResult.Text = "Date2 must be after Date1";
				else
				{
					//do whatever here
				}
			}
			else lblResult.Text = "Invalid date format. Please check your input in the Date1 and Date2 fields";
		}
 
0
 

Thats not good programming and performance!
NEVER use try catch as validation! IT is huge performance overhead.
What is wrong with DateTime.TryParse????

 
0
 

1) DateTime.TryParse is only available in 2.0
2) I believe that if you dig into the specs for TryParse, at it's root it is a try/catch block anyway

 
0
 

Ahhh my bad. Been using 2.0 for too long i guess :)
I still dont recommend a try catch (and am going to look into the tryparse as it is expensive to use try catchs.

In that case write a simple utility (I have them for integers, decimal etc) that uses regular expressions to validate the date

Regx.ValidationExpression=@"^(([1-9])|(0[1-9])|(1[0-2]))\/((0[1-9])
|([1-31]))\/((\d{2})|(\d{4}))$";

if it is a match then it is a date and so can perform quickly the day is valid for the month. It will still be a lot quicker than try catch blocks

 
0
 

oops i didnt realize they wanted it in dd/mm format not mm/dd format... just flick round the regex parts for days and months

 
0
 

Dear Frd,

Just try

dd/mm/yyyy Format In regular Expression (optional mm,optional dd)
([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.][0-9]{4}$


mm/dd/yyyy (optional mm,optional dd)
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$

mm/dd/yyyy (Exact Format)
^([01]\d)/([0-3]\d)/(\d{4})$


Regular Expression in ASP.net

ASP.net Discussion

 
0
 

You can use any culture that supports the dd-mm-yy format like the french one, for example to format the date time now in a format dd-mm-yy i can do as follow:

cultureInfo culture = new cultureinfo("fr-FR");
string oFormatedDate = dtNow.ToString("d", culture);

but the Ketanbece solution is better even it uses the regluar expressions

 
0
 

if you need the date string only in dd/mm/yyyy format not dd.mm.yyyy or dd-mm-yyyy this Reguler Expression Validator can be helpfull

<asp:RegularExpressionValidator ID="REcreationdate" runat="server" ControlToValidate="txtCreationDate"
Display="Dynamic" ErrorMessage="Date should be in dd-mm-yyyy format" SetFocusOnError="True"
ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /](0[1-9]|1[012])[- /](19|20)[0-9]{2}"
ValidationGroup="v1"></asp:RegularExpressionValidator>

 
0
 

First use regular Expression then write this ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$ in validation Expression property.Dont forget to write d name of textbox in

 
0
 

can anybody tell what DateTime(1,1,1) means??

 
0
 

dd/mm/yyyy Format In regular Expression (optional mm,optional dd)
([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.][0-9]{4}$


mm/dd/yyyy (optional mm,optional dd)
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}$

mm/dd/yyyy (Exact Format)
^([01]\d)/([0-3]\d)/(\d{4})$

 
0
 

DateTime dt = DateTime.Now;
TextBox1.Text = dt.ToString("dd/MM/yyyy");
//try this code

 
0
 

DateTime dt = DateTime.Now;
TextBox1.Text = dt.ToString("dd/MM/yyyy");
//try this code

To display Date Format as:"Sunday,12 September 2010"
You Can Use follwoing code:-
DateTime dt = DateTime.Now;
TextBox1.Text = dt.ToString("dddd,dd MMMM yyyy");

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article