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.