To convert a time string from a custom time format to C # standard DateTime time

Source: Internet
Author: User

Today, there is a problem that throws a formatexception when "YYYYMMDDHHMMSS" is converted directly with Convert.todatetime (): The string is not recognized as a valid DateTime.

To search the Internet, found a solution:

        stringTime1 ="20150123134045"; DateTime DT1= DateTime.ParseExact (time1,"YYYYMMDDHHMMSS", CultureInfo.CurrentCulture); stringTime2 ="2015012313404500"; DateTime DT2= DateTime.ParseExact (Time2,"YYYYMMDDHHMMSSFF", CultureInfo.CurrentCulture); stringTime3 ="201501231340450000"; DateTime DT3= DateTime.ParseExact (Time3,"yyyymmddhhmmssffff", CultureInfo.CurrentCulture); stringTime4 ="2015,01,23,134045"; DateTime DT4= DateTime.ParseExact (Time4,"Yyyy,mm,dd,hhmmss", CultureInfo.CurrentCulture);

DateTime.ParseExact This method supports the process of turning a custom time format into a standard datetime time. I have tried four kinds, others can try.

By the way, you can get all the time formats for C #:

        New DateTimeFormatInfo ();         string [] arr =  formatinfo. Getalldatetimepatterns ();    

To convert a time string from a custom time format to C # standard DateTime time

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.