c# DateTime.ParseExact fractional seconds issue
I have a scenario where I'm reading values from a file and converting it to DateTime
. The problem is that in my time, sometimes I have 5 decimal places for seconds and sometimes 6.
For eg, 19:23:30.12345
and 12:13:33.123456开发者_运维技巧
. If I have to parse this, I cannot use ss.fffff
or ss.ffffff
. What do I do?
Try using six capital F's. According to the documentation under The F Custom Format Specifier heading:
The number of "F" format specifiers used with the ParseExact, TryParseExact, ParseExact, or TryParseExact method indicates the maximum number of most significant digits of the seconds fraction that can be present to successfully parse the string.
Why is anything special needed? Doesn't DateTime.Parse(stringFromFile) work directly, without a specifier?
精彩评论