时间跨度格式不正确

发布于 2024-08-20 04:54:50 字数 257 浏览 4 评论 0原文

我目前正在尝试转换给定时间(通过文本框输入),输入的时间看起来有点像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-08-27 04:54:50

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

虫児飞 2024-08-27 04:54:50

要解析的字符串的规范是

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

其中 ws 是空格,d 是从 0 到 10675199 的天数,其余的含义很明显(如果您不知道)如何阅读这样的规范,方括号中的项目是可选的,并且必须从大括号内的项目中选择一项1)。因此,如果您想将 "01 52 22" 解析为 TimeSpan,其中 TimeSpan.Minutes == 1TimeSpan.Seconds == 52TimeSpan.Milliseconds == 22 那么您需要将输入重新格式化为 "00:01:52.22" 并解析

字符串 s = " 00:01:52.22";
TimeSpan t = TimeSpan.Parse(s);

或者像这样自己解析字符串

string s = "01 52 22";
string[] fields = s.Split(' ');
int minutes = Int32.Parse(fields[0]);
int seconds = Int32.Parse(fields[1]);
int milliseconds = Int32.Parse(fields[2]);
TimeSpan t = new TimeSpan(0, 0, minutes, seconds, millseconds);

如果我在文本框中输入类似 46 的内容,它就可以工作,但随后它将天数设置为 46 而不是秒。

因此,参考上面的规范,"46" 解析为具有 TimeSpan.Days == 46TimeSpan 的原因是因为查看规范再次

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

没有空格,没有 -,没有尾随空格,我们减少查看

d

or

[d.]hh:mm[:ss[.ff]]

"46" 显然符合以前的规范,因此可以按照您的方式进行解析见过。

1:帮自己一个忙,学习正则表达式;虽然上面的内容不是正则表达式,但理解它们将帮助您阅读上面的规范。我推荐掌握正则表达式。理解正式语法也有帮助。

The specification for the string to be parsed is

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

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 a TimeSpan with TimeSpan.Minutes == 1, TimeSpan.Seconds == 52 and TimeSpan.Milliseconds == 22 then you either need to reformat your input to "00:01:52.22" and parse

string s = "00:01:52.22";
TimeSpan t = TimeSpan.Parse(s);

or parse the string yourself like so

string s = "01 52 22";
string[] fields = s.Split(' ');
int minutes = Int32.Parse(fields[0]);
int seconds = Int32.Parse(fields[1]);
int milliseconds = Int32.Parse(fields[2]);
TimeSpan t = new TimeSpan(0, 0, minutes, seconds, millseconds);

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.

Thus, referring to the specification above, the reason that "46" parses as a TimeSpan with TimeSpan.Days == 46 is because looking at the specification again

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

there is no whitespace, no -, no trailing whitespace and we reduce to looking at

d

or

[d.]hh:mm[:ss[.ff]]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文