I know this has halready had a response marked as the answer, but I was thinking it might be fun to look at it from another viewpoint...
First
TimeSpan
is represented internally as a
long
(just get the
TimeSpan.Ticks
property, and Bob's your uncle), so it's already an integer type.
Second, you could convert it to a decimal, just for the coding exercise:
public static class ExtensionMethods
{
public static decimal ToDecimal(this TimeSpan span)
{
decimal spanSecs = (span.Hours * 3600) + (span.Minutes * 60) + span.Seconds;
decimal spanPart = spanSecs / 86400M;
decimal result = span.Days + spanPart;
return result;
}
public static TimeSpan ToTimeSpan(this decimal value)
{
int days = Convert.ToInt32(Math.Ceiling(value));
value -= days;
int time = Convert.ToInt32(value * 86400M);
TimeSpan result = new TimeSpan(1, 0, 0, time, 0);
return result;
}
}
Usage would be thus:
TimeSpan span = new TimeSpan(0,12,0,0,0);
decimal spanAsDecimal = span.ToDecimal();
TimeSpan span2 = spanAsDecimal.ToTimeSpan();
Useful? Maybe, maybe not. Fun to code? Yep.
Caveat: This doesn't take milliseconds into account, but it would be easy to add.