DateTime.ParseExact(String, String, C# with Example



DateTime.ParseExact(String, String, C# with Example

IFormatProvider) 
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 

0 Comment's

Comment Form

Submit Comment