Home > database >  C# String to DateTime format not recognised
C# String to DateTime format not recognised

Time:01-18

I've got the following code.


string date = "2022-01-17-21:00:18.661";
format = "yyyy-MM-dd-hh:mm:ss.fff";
CultureInfo provider = CultureInfo.InvariantCulture;
timetamp = DateTime.ParseExact(date, format, provider);

For any reason, an exception with the message "String was not recognized as a valid DateTime." is thrown all the time. Additionally, I tried to write format = "yyyy'-'MM'-'dd'-'hh':'mm':'ss'.'fff";, but it did not help.

CodePudding user response:

The format string isn't correct - for the hours component - for the given the date format. h or hh converts using 12 hour format, while HH converts using 24 hour format.

The correct format string would be:

format = "yyyy-MM-dd-HH:mm:ss.fff" 
  •  Tags:  
  • Related