Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string date="23-06-1999";


I have a date like this, now my requirement is i want to check the condition if the date is in "MM-dd-yy" format or not.

If the date is in the same format insert the records otherwise i want to display error message...


Can any body help me out of this...
Posted

Hi,

along with DateTime.TryParseExact, you can make use of regular expressions.
use below expression for mm-dd-yyyy.
/^(0[1-9]|1[012])[/ -.](0[1-9]|[12][0-9]|3[01])[/ -.](19|20)\d\d+$/

refer article The 30 Minute Regex Tutorial[^] for more on regular expressions.

hope it helps.
 
Share this answer
 
Comments
Maciej Los 13-Feb-13 7:03am    
Good answer! +5!
Karthik Harve 13-Feb-13 7:05am    
Thanks Maciej. :-)
You can use DateTime.TryParseExact method and pass the appropriate dateformat. If it succeeds you can do your further processing.

Go through the below MSDN link for more info.
http://msdn.microsoft.com/en-us/library/ms131044.aspx[^]

All the best.
 
Share this answer
 
Just use DateTime.TryParseExact Method[^].
You can optionally check if formatted parsed date is equal to input string.

Or you can use simple regex like this:
^\d{2}-\d{2}-20\d{2}$
In case you want to check if input is in 100% correct format, you should extend this pattern.
 
Share this answer
 

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