【发布时间】:2014-11-01 07:49:13
【问题描述】:
我通过查询字符串将字符串作为时间单位传递。但是当我尝试将字符串解析为时间跨度对象时,我得到一个 System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code,我收集这意味着我格式化要解析的字符串的方式存在问题。
if (NavigationContext.QueryString.ContainsKey("workTimeSpanPkr"))
{
testString = NavigationContext.QueryString["workTimeSpanPkr"];
//Assign text box string value to a test time span variable.
testTm = TimeSpan.ParseExact(testString, @"hh\ \:\ mm\ \:\ ss", CultureInfo.InvariantCulture);
}
当我通过调试器运行 testString 时传递的字符串是:`"00:15:04"``
有人知道解析小时、分钟和秒的正确格式吗?
这是我要解析的值以及我用来实现此目的的代码:
【问题讨论】:
-
为什么字符串文字 (@) 上的所有转义序列?听起来解析格式应该是
"hh:mm:ss"。 -
真的是这样引用的吗? - 单,双,字符串,双,单,单 - 这是不对的
-
通过这么简单的解析,您可能只需要使用
TimeSpan.Parse或TimeSpan.TryParse -
@BradleyDotNET 一个常见的错误。DateTime 和 TimeSpan 格式不一样。 msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx
-
@L.B 每天学习新东西。感谢您的链接!
标签: c# parsing timespan string-formatting