Is there a tidy way of doing this rather than doing a split on the colon's and multipling out each section the relevant number to calculate the seconds?
+13
A:
It looks like a timespan. So simple parse the text and get the seconds.
string time = "00:01:05";
double seconds = TimeSpan.Parse(time).TotalSeconds;
michl86
2009-03-25 15:15:23
Why are you storing seconds in a double?
Alan
2009-03-25 15:19:16
The property TotalSeconds of TimeSpan is double - i don't know why...
michl86
2009-03-25 15:20:13
Cuz it supports fractional seconds. Interesting. Thx.
Alan
2009-03-25 15:21:17
Now i know why! Expect the following TimeSpan: "1:30:45.12". Then the result is 5445.12.
michl86
2009-03-25 15:21:40
cheers mich186 :)
Andi
2009-03-25 15:39:25
The TotalSeconds is a double because it returns the entire TimeSpan as seconds, where as the Seconds property only returns the actual seconds, which were what was asked for.
Arkain
2009-03-25 23:19:58
+7
A:
TimeSpan.Parse() will parse a formatted string.
So
TimeSpan.Parse("03:33:12").TotalSeconds;
Alan
2009-03-25 15:15:31
+9
A:
You can use the parse method on aTimeSpan.
http://msdn.microsoft.com/en-us/library/system.timespan.parse.aspx
TimeSpan ts = TimeSpan.Parse( "10:20:30" );
double totalSeconds = ts.TotalSeconds;
The TotalSeconds property returns the total seconds if you just want the seconds then use the seconds property
int seconds = ts.Seconds;
Seconds return '30'. TotalSeconds return 10 * 3600 + 20 * 60 + 30
Arkain
2009-03-25 15:17:50