C# 为什么 TimeSpan.ParseExact 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11719055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does TimeSpan.ParseExact not work
提问by Quango
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.
第二次解析失败,出现异常“输入字符串的格式不正确”。从日期时间。
采纳答案by Jon
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.
格式字符串中的任何其他未转义字符(包括空白字符)都被解释为自定义格式说明符。在大多数情况下,任何其他未转义字符的存在都会导致 FormatException。
有两种方法可以在格式字符串中包含文字字符:
将其括在单引号中(文字字符串分隔符)。
在它前面加上一个反斜杠 ("\"),它被解释为转义字符。这意味着,在 C# 中,格式字符串必须用 @ 引用,或者文字字符前面必须有一个额外的反斜杠。
.NET Framework 没有为时间间隔中的分隔符定义语法。这意味着天和小时、小时和分钟、分钟和秒以及秒和秒的小数之间的分隔符都必须被视为格式字符串中的字符文字。
So, the solution is to specify the format string as
因此,解决方案是将格式字符串指定为
TimeSpan.ParseExact(tmp, "hh\:mm\:ss", CultureInfo.InvariantCulture)
回答by V4Vendetta
It seems that HH is not really for TimeSpan
看来HH真的不是为了 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.
自定义 TimeSpan 格式说明符不包括占位符分隔符,例如将天与小时、小时与分钟或秒与小数秒分开的符号。相反,这些符号必须作为字符串文字包含在自定义格式字符串中。例如,“dd.hh\:mm”将句点 (.) 定义为天和小时之间的分隔符,将冒号 (:) 定义为小时和分钟之间的分隔符。
Hence the correct way would be as Jon mentioned to escape using "\"Read More
因此,正确的方法是乔恩提到的使用“\”转义阅读更多
Your TimeSpanis "17:23:24"which is in the 24 hour format and it should be parsed using HHformat and not hhwhich is for 12 hour formats.
您TimeSpan是“17:23:24”,它采用 24 小时格式,应该使用HHformat 而不是hh12 小时格式解析。
TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
TimeSpan.ParseExact(tmp, "HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Check out the formats
查看格式
回答by speti43
Try this:
尝试这个:
var t2 = TimeSpan.ParseExact(tmp, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
回答by M. Mennan Kara
Try this:
尝试这个:
var t2 = TimeSpan.ParseExact(tmp, "c", System.Globalization.CultureInfo.InvariantCulture);
Source: Standard TimeSpan Format Strings
来源: 标准时间跨度格式字符串
回答by samgak
If you don't want to deal with the difference in format specifiers between TimeSpan.ParseExactand DateTime.ParseExactyou can just parse your string as a DateTimeand get the TimeOfDaycomponent as a TimeSpanlike this:
如果您不想处理 和 之间格式说明符的差异TimeSpan.ParseExact,DateTime.ParseExact您可以将字符串解析为 aDateTime并像这样获取TimeOfDay组件TimeSpan:
var t2 = DateTime.ParseExact(tmp, "hh:mm:ss", CultureInfo.InvariantCulture).TimeOfDay;

