Why does TimeSpan.ParseExact not work

廉价感情. 提交于 2019-12-17 06:49:18

问题


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.Parse(tmp);
//fails
var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

The second parse fails with an exception "Input string was not in a correct format." from DateTime.


回答1:


From the documentation:

Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. In most cases, the presence of any other unescaped character results in a FormatException.

There are two ways to include a literal character in a format string:

  • Enclose it in single quotation marks (the literal string delimiter).

  • Precede it with a backslash ("\"), which is interpreted as an escape character. This means that, in C#, the format string must either be @-quoted, or the literal character must be preceded by an additional backslash.

The .NET Framework does not define a grammar for separators in time intervals. This means that the separators between days and hours, hours and minutes, minutes and seconds, and seconds and fractions of a second must all be treated as character literals in a format string.

So, the solution is to specify the format string as

TimeSpan.ParseExact(tmp, "hh\\:mm\\:ss", CultureInfo.InvariantCulture)



回答2:


Try this:

var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);

Source: Standard TimeSpan Format Strings




回答3:


If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExact and DateTime.ParseExact you can just parse your string as a DateTime and get the TimeOfDay component as a TimeSpan like this:

var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;



回答4:


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




回答5:


Try this:

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


来源:https://stackoverflow.com/questions/11719055/why-does-timespan-parseexact-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!