I want to check if a date has a correct format. There is many possibilities of correct dates like:
- 02.08.2010
- 2.8.2010
- 02.8.2010 02.08
- 02.August
- ...
I can test each on with code like this:
if (DateTime.TryParse(DateTime.ParseExact(date, "dd.M.",
new CultureInfo("sl-SI")).ToString(), out dt))
But then I can have 40 if statements. Is it possible to check all dates with one if statement or one loop?
Update:
Based on the answers so far, I am testing this code, but I have one more problem. What if I have just 9.2 not 9.2.2010 then this code will not work:
CultureInfo ci = CultureInfo.GetCultureInfo("sl-SI");
string[] fmts = ci.DateTimeFormat.GetAllDateTimePatterns();
if (DateTime.TryParseExact(date, fmts, ci, DateTimeStyles.AssumeLocal, out dt))
{
DateTime = Convert.ToDateTime(date);
Check = true;
}
Must I manually add this times or what can I do?