Click here to Skip to main content
12,076,727 members (46,339 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# 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 13-Feb-13 7:03am
   
Good answer! +5!
Karthik Harve 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2016
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