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