.NET Framework DateTime parsing TryParseExact

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

This method behaves as a combination of TryParse and ParseExact: It allows custom format(s) to be specified, and returns a Boolean result indicating success or failure rather than throwing an exception if the parse fails.

TryParseExact(string, string, IFormatProvider, DateTimeStyles, out DateTime)

This overload attempts to parse the input string against a specific format. The input string must match that format in order to be parsed.

DateTime.TryParseExact("11242015", "MMddyyyy", null, DateTimeStyles.None, out parsedValue); // true

TryParseExact(string, string[], IFormatProvider, DateTimeStyles, out DateTime)

This overload attempts to parse the input string against an array of formats. The input string must match at least one format in order to be parsed.

DateTime.TryParseExact("11242015", new [] { "yyyy-MM-dd", "MMddyyyy" }, null, DateTimeStyles.None, out parsedValue); // true


Got any .NET Framework Question?