Click here to Skip to main content
11,493,233 members (60,050 online)
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 13-Feb-13 0:07am
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 Sergey Alexandrovich Kryukov 393
1 Maciej Los 286
2 F-ES Sitecore 224
3 KarstenK 210
4 CPallini 208
0 Sergey Alexandrovich Kryukov 10,132
1 OriginalGriff 8,630
2 Sascha Lefèvre 3,624
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2015
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