I want to check if the date matches the correct format. There are many possibilities for valid dates like:
- 08/02/2010
- 2.8.2010
- 08/02/2010 02.08
- 02.August
- ...
I can test each code with this code:
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 a single if statement or a single loop?
Update:
Based on the answers so far, I'm testing this code, but I have one more problem. What if I only have 9.2 not on 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; }
Do I have to manually add this time or what can I do?
c # datetime
senzacionale
source share