Time divided by hours/minutes
How can I divide time by using intervals?
like 01:00 divided by 20 mins = 3?
06:00 divided by 2 hours = 开发者_如何学编程3?
/M
I'd just use the TimeSpan object:
int hours = 1;
int minutes = 0;
int seconds = 0;
TimeSpan span = new TimeSpan(hours, minutes, seconds);
double result = span.TotalMinutes / 20; // 3
Don't bother manually doing any conversions, the TimeSpan
object with it's TotalHours
, TotalMinutes
, TotalSeconds
properties, etc, do it all for you.
Something like this should work well, I suppose:
public static double SplitTime(TimeSpan input, TimeSpan splitSize)
{
double msInput = input.TotalMilliseconds;
double msSplitSize = splitSize.TotalMilliseconds;
return msInput / msSplitSize;
}
Example; split 1 hour in 20 minute chunks:
double result = SplitTime(new TimeSpan(1,0,0), new TimeSpan(0,20,0));
I guess the method could fairly easily be reworked to return an array of TimeSpan
objects containing the different "slices".
First convert everything to seconds. 01:00 => 3600 seconds, 20 mins => 1200 seconds then you can divide
Convert to minutes and then do the divison.
h - hours
m - minutes
hd - divider hours
md - divider minutes
(h * 60 + m) / (hd * 60 + md)
精彩评论