Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# String to DateTime format not recognised

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading