Why does TimeSpan.ParseExact not work

前端 未结 5 1033
既然无缘
既然无缘 2020-11-29 10:00

This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise!

const string tmp = \"17:23:24\";
//works
var t1 = TimeSpan.Pars         


        
5条回答
  •  借酒劲吻你
    2020-11-29 10:42

    It seems that HH is not really for TimeSpan

    The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd.hh\:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

    Hence the correct way would be as Jon mentioned to escape using "\" Read More

    Your TimeSpan is "17:23:24" which is in the 24 hour format and it should be parsed using HH format and not hh which is for 12 hour formats.

    TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
    

    Check out the formats

提交回复
热议问题