You can use the TryParseExact method, which allows you to pass a collection of possible formats that you want to support. The TryParse method is TryParse dependent, so be very careful if you decide to use it.
So for example:
DateTime fromDateValue; string s = "15/07/2012"; var formats = new[] { "dd/MM/yyyy", "yyyy-MM-dd" }; if (DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out fromDateValue)) { // do for valid date } else { // do for invalid date }
Darin Dimitrov
source share