Code has been simplified for ease of asking question, string is pulled from an array and has been verified to be exactly as provided below. The below code results in error:
Message: "String was not recognized as a valid DateTime."
string myDate = "20221215.08.14.01.37";
DateTime dt = DateTime.Parse(myDate);
I was expecting the string to be converted to DateTime, have tried working with other DateTime methods such as TryParse, and providing formatting, without success.
CodePudding user response:
This is unusual format so Parse
cannot handle that. You have to use DateTime.ParseExact
:
DateTime.ParseExact(myDate, "yyyyMMdd.HH.mm.ss.ff", CultureInfo.InvariantCulture);
See more info about format specifiers here.