时间跨度格式不正确
我目前正在尝试转换给定时间(通过文本框输入),输入的时间看起来有点像 01 52 22 分钟秒米秒。
但是 Timespan.parse(tbName.text) 给了我一个不正确的格式错误。
如果我在文本框中输入类似 46 的内容,它就可以工作,但随后它将天数设置为 46 而不是秒。
有什么想法如何从上述文本输入中设置分钟秒和毫秒?
我很确定时间跨度是可行的方法,但我读过的许多帖子都使用日期时间,并且仅通过格式化使用变量的时间部分
I am currently tryibng to convert a given time(Entered via a text box) , The time entered would look a little like 01 52 22 mins secs mili secs.
however Timespan.parse(tbName.text) gives me an incorrect format error.
I have got it to work if i input something like 46 in to the textbox but then it sets the days to 46 not the seconds.
Any ideas how to get it just to set the mins seconds and mili seconds from the text input stated above?
I am pretty sure timespan is the way to go but many posts i have read use the dateTime and only use the time part of the variable via formatting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MDSN 上的文档非常精彩,您是否先去那里看看?
http://msdn.microsoft.com/en-us/library/se73z7b9。 ASPX
Wonderful docs on MDSN, did you bother looking there first?
http://msdn.microsoft.com/en-us/library/se73z7b9.aspx
要解析的字符串的规范是
其中
ws
是空格,d
是从 0 到 10675199 的天数,其余的含义很明显(如果您不知道)如何阅读这样的规范,方括号中的项目是可选的,并且必须从大括号内的项目中选择一项1)。因此,如果您想将"01 52 22"
解析为TimeSpan
,其中TimeSpan.Minutes == 1
、TimeSpan.Seconds == 52
和TimeSpan.Milliseconds == 22
那么您需要将输入重新格式化为"00:01:52.22"
并解析字符串 s = " 00:01:52.22";
TimeSpan t = TimeSpan.Parse(s);
或者像这样自己解析字符串
因此,参考上面的规范,
"46"
解析为具有TimeSpan.Days == 46
的TimeSpan
的原因是因为查看规范再次没有空格,没有
-
,没有尾随空格,我们减少查看or
和
"46"
显然符合以前的规范,因此可以按照您的方式进行解析见过。1:帮自己一个忙,学习正则表达式;虽然上面的内容不是正则表达式,但理解它们将帮助您阅读上面的规范。我推荐掌握正则表达式。理解正式语法也有帮助。
The specification for the string to be parsed is
where
ws
is whitespace,d
is days from 0 to 10675199 and the meaning of the rest is obvious (if you don't know how to read such a specification, items in square brackets are optional, and one item must be chosen from the items inside curly braces1). Thus, if you want to parse"01 52 22"
as aTimeSpan
withTimeSpan.Minutes == 1
,TimeSpan.Seconds == 52
andTimeSpan.Milliseconds == 22
then you either need to reformat your input to"00:01:52.22"
and parsestring s = "00:01:52.22";
TimeSpan t = TimeSpan.Parse(s);
or parse the string yourself like so
Thus, referring to the specification above, the reason that
"46"
parses as aTimeSpan
withTimeSpan.Days == 46
is because looking at the specification againthere is no whitespace, no
-
, no trailing whitespace and we reduce to looking ator
and
"46"
clearly fits the former specification and thus parses as you've seen.1: Do yourself a favor and learn regular expressions; while the above is not a regular expression, understanding them will help you read specifications like the above. I recommend Mastering Regular Expressions. Understanding formal grammars helps too.