In C # if it is convenient to convert a date of a string type to a date type
that usesconvert.todatetime ("2015/01/01"). ToString () orDatetime.tryparse The conversion can be completed if the format of the string must be a date format that the system can recognizesuch as:YYYY-MM-DDYYYY/MM/DDand so on ....if the format in the string is custom (YYYYMMDD), thenSystem ofmethod cannot complete the conversion directly (although the content of the string is a date, such as 20111021)stillGood C # provides powerful, customizable format conversion capabilities, you can complete the custom requirements, no nonsense directly on the codeOnline Demo
/****************************************************************** * Creator: HTL * creation time: 2015-04-08 15:36:35 * Description: C # Use Datetime.tryparseexact to convert a custom date format of a string to a date type * DEMO URL:Http://ideone.com/I6MuaZ* Email:[email protected] *******************************************************************/usingSystem; Public classdatetime_tryparseexact_demo{ Public Static voidMain () {stringstr = DateTime.Now.ToString ("YYYYMMDD"); string[] format = {"YYYYMMDD"}; DateTime date; if(datetime.tryparseexact (str, format, System.glo Balization. CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, outdate)) {Console.WriteLine ("Custom DateTime Type Convert success:"+date. ToString ()); } ElseConsole.WriteLine ("Custom DateTime Type Convert error"); }//End Main}//End
There is a picture of the truth: Reference: Online DemoMSDN:DateTime.TryParseExact method Stack Overflow:convert YYYYMMDD string date to a DateTime value [duplicate] Cnblogs.com:c# of the language "date time string in string format to datetime type" method
From for notes (Wiz)
C # uses Datetime.tryparseexact to convert custom date types to dates