Click here to Skip to main content
14,332,414 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,i know how to validate date as dd/mm/yyyy format in asp.net but now i want to validate date as dd/mm/yyyy format in window how can i do this please help
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 4

John Simmons gave you a good start, now add the IFormatProvider and the DateTimeStyles to the DateTime.TryParse method, or in the other hand, you can try to add 3 separate boxes, one for day, one more for month and the last one for year
   
Comments
Albin Abel 7-Mar-11 23:04pm
   
Yes. He even don't want to search how to use tryparse. My 5
Rate this:
Please Sign up or sign in to vote.

Solution 2

You could use DateTime.TryParse. If it returns true, the date is valid.
   
Comments
software_Engi08 7-Mar-11 16:14pm
   
i have written code as DateTime dob;
//if (!DateTime.TryParse(maskedTextBox1.Text, out dob))
//{
// MessageBox.Show("invalid date");
//}
but this is mm/dd/yyyy format.i want dd/mm/yyyy format
Gonzoox 7-Mar-11 16:44pm
   
this is a good start, +5
Albin Abel 7-Mar-11 23:02pm
   
This is correct. My 5
Rate this:
Please Sign up or sign in to vote.

Solution 1

i think try with masked text box it will helps you
try to validate with masked text box
   
Comments
software_Engi08 7-Mar-11 16:16pm
   
i have used masktext box as mask=00/00/0000.but this is in mm/dd/yyyy format .i want dd/mm/yyyy format .please help
Albin Abel 7-Mar-11 23:03pm
   
Masked Textbox won't validate date format
Rate this:
Please Sign up or sign in to vote.

Solution 3

First of all, "mm" means minutes, "MM" means months! And now:
DateTime dt;
bool valid = DateTime.TryParseExact(text, "dd/MM/yyyy", null, DateTimeStyles.None, out dt);

This should do the job.
   
v2
Comments
Member 11428767 7-Feb-15 2:04am
   
its not working with windows form
Rate this:
Please Sign up or sign in to vote.

Solution 5

Hi
John is correct. It is your problem don't know how to use Tryparse

See that function accepts a format.
DateTime dob;
DateTimeFormatInfo info = new DateTimeFormatInfo();
info.ShortDatePattern = "dd/MM/yyyy";
DateTime.TryParse("30/3/2011", info, DateTimeStyles.None, out dob);


This way it works. You are ready to search what tryparse doing, instead you are simply looking for ready answer. Better try something.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100