Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
 
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 12-Feb-13 23:07pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Comments
Maciej Los at 13-Feb-13 7:03am
   
Good answer! +5!
Karthik Harve at 13-Feb-13 7:05am
   
Thanks Maciej. :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,400
2 CPallini 5,240
3 George Jonsson 3,584
4 Gihan Liyanage 2,625


Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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