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
?
Use an array of valid dates format, check docs:
I think one of the solutions is to use DateTime.ParseExact or DateTime.TryParseExact
source: http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
you could always try:
this will check that the string is in the format "02/02/2002" you may need a bit more if you want to ensure that it is a valid date like dd/mm/yyyy
https://msdn.microsoft.com/es-es/library/h9b85w22(v=vs.110).aspx
Datetime dt=datetime.parse(string); string out=dt.tostring("dd/MM/yyyy");