Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi

I have string variable as below.

string date="33/22/2011" (mm/dd/yyyy)

i want to validate whether date variable is correct date or not.

Regards,
Nagaraju
Posted
Comments
R. Erasmus 25-Mar-11 4:26am    
programming level 1... try to write a simple validation function yourself.
If (month < 12)... and so on.

1 solution

Here is the example:

DateTime someDateTime;
var dateTimeString = "99/23/56";
var isValid = DateTime.TryParse(dateTimeString, out someDateTime);


Vote if my answer will help you.
 
Share this answer
 
Comments
BobJanova 24-Mar-11 11:39am    
Yes, using the DateTime methods is a good approach. Perhaps using DateTime.TryParseExact might be better if the OP knows the supposed format of the date in advance.
wizardzz 24-Mar-11 12:15pm    
Exactly Bob, Pavel, your method can lead to dates being validated that do not match the intended format.
Sergey Alexandrovich Kryukov 24-Mar-11 14:47pm    
You need to validate with required culture to make it correct, therefore only 4.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900