.NET: Why is TryParseExact failing on Hmm and Hmmss?

后端 未结 6 785
闹比i
闹比i 2020-12-03 22:07

I\'m trying out the DateTime.TryParseExact method, and I have come over a case that I just don\'t get. I have some formats and some subjects to parse that each

6条回答
  •  孤城傲影
    2020-12-03 22:17

    To quote from MSDN's Using Single Custom Format Specifiers:

    A custom date and time format string consists of two or more characters. For example, if the format string consists only of the specifier h, the format string is interpreted as a standard date and time format specifier. However, in this particular case, an exception is thrown because there is no h standard date and time format specifier.

    To use a single custom date and time format specifier, include a space before or after the date and time specifier, or include a percent (%) format specifier before the single custom date and time specifier. For example, the format strings "h " and "%h" are interpreted as custom date and time format strings that display the hour represented by the current date and time value. Note that, if a space is used, it appears as a literal character in the result string.

    So, should that have been % H in the first element in the formats array?

    Hope this helps, Best regards, Tom.

提交回复
热议问题