C # DateTime.TryParseExact using custom type conversion to date Date

 

In C # If a string is converted into a type of date date type easily

I.e. using Convert.ToDateTime ( "2015/01/01"). ToString () or DateTime.TryParse can complete the conversion, provided that the string must be in a format recognizable by the system date format
Such as:
yyyy-MM-dd
yyyy/MM/dd
and many more....
If the string is custom format if (yyyyMMdd), then the system's method for direct conversion can not be completed (although the content is the date string, such as 20111021)
Fortunately, C # provides a powerful customizable format conversion function, you can complete the customization needs, not crap on direct Code Online Demo
Copy the code
/ * **************************** ***************** 
 * created: the HTL 
 * created: 2015-04-08 15:36:35 
 custom C # using DateTime.TryParseExact the string: * Description date format converted into a date type 
 * the DEMO the Url: http://ideone.com/I6MuaZ  * In Email: [email protected]  *********************** ******************************************* * / a using System; public class {DateTime_TryParseExact_Demo public static void the Main () { String = DateTime.Now.ToString STR ( " yyyyMMdd " ); String [] = {the format " yyyyMMdd " }; the DateTime DATE;if (DateTime.TryParseExact(str, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date)) { Console.WriteLine("Custom DateTime Type Convert success:"+date.ToString()); } else Console.WriteLine("Custom DateTime Type Convert error "); }//end Main }//end 
Copy the code

 

There are pictures and the truth:
 
reference:

Guess you like

Origin www.cnblogs.com/roboot/p/11387828.html