I want to check whether a string
contains dates such as 1/01/2000
and 10/01/2000
in dd/MM/yyyy
format.
So far I have tried this.
DateTime dDate = DateTime.Parse(inputString);
string.Format("{0:d/MM/yyyy}", dDate);
But how can I check if that format is correct to throw an exception
?
Try this
see this link for more info. http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
you can use
DateTime.ParseExact
with the format stringAbove will throw an exception if the given string not in given format.
use
DateTime.TryParseExact
if you don't need exception in case of format incorrect but you can check the return value of that method to identify whether parsing value success or not.check Custom Date and Time Format Strings