C# parse timestampwith format "yyyyMMdd HH:mm:SS.ms"

Abstract picture Abstract · Mar 28, 2012 · Viewed 51.3k times · Source

I wanted to format a string to dateTime with the format

"yyyyMMdd HH:mm:SS.ms"

I tried doing "yyyyMMdd HH:mm:SS" as the string format for ParseExact but it doesn't recognise it. Also no clue how to include the milliseconds as well

any assistance?

Answer

Jon Skeet picture Jon Skeet · Mar 28, 2012

From the custom date and time format strings page, you use ss for seconds, and FFF for milliseconds:

"yyyyMMdd HH:mm:ss.FFF"

or

"yyyyMMdd HH:mm:ss.fff"

(Use the first if trailing 0s are suppressed, the second otherwise.)