DateTime.ParseExactStringStringIFormatProvider

suggest change

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

string str = "08-07-2016 11:30:12 PM";
DateTime date = DateTime.ParseExact(str, "MM-dd-yyyy hh:mm:ss tt", CultureInfo.CurrentCulture);

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

Let’s say we have a DateTime string in dd-MM-yy hh:mm:ss tt format and we want it to convert to equivalent DateTime object, without any specific culture information

string str = "17-06-16 11:30:12 PM";
DateTime date = DateTime.ParseExact(str, "dd-MM-yy hh:mm:ss tt", CultureInfo.InvariantCulture);

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

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

NOTE : System.Globalization needs to be added for CultureInfo Class

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


DateTime:
* DateTime.ParseExactStringStringIFormatProvider

Table Of Contents
17 Regex
18 DateTime
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script