DateTime.ParseExactStringStringIFormatProvider

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

Convert a specific format string to equivalent DateTime

Let’s say we have a culture-specific DateTime string 08-07-2016 11:30:12 PM as MM-dd-yyyy hh:mm:ss tt format and we want it to convert to equivalent DateTime object

Convert a date time string to equivalent DateTime object without any specific culture format with different format

Let’s say we have a Date string , example like ‘23-12-2016’ or ‘12/23/2016’ and we want it to convert to equivalent DateTime object, without any specific culture information

stringdate='23-12-2016'ordate=12/23/2016';string[]formats=newstring[]{"dd-MM-yyyy","MM/dd/yyyy"};// even can add more possible formats.
DateTimedate=DateTime.ParseExact(date,formats,CultureInfo.InvariantCulture,DateTimeStyles.None);