C # uses Datetime.tryparseexact to convert custom date types to dates

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.