Lasse Edsvik
Lasse Edsvik

Reputation: 9298

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

Upvotes: 2

Views: 5945

Answers (4)

djdd87
djdd87

Reputation: 68456

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.

Upvotes: 8

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

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".

Upvotes: 4

nuriaion
nuriaion

Reputation: 2631

First convert everything to seconds. 01:00 => 3600 seconds, 20 mins => 1200 seconds then you can divide

Upvotes: 2

Egon
Egon

Reputation: 1705

Convert to minutes and then do the divison.

h - hours
m - minutes
hd - divider hours
md - divider minutes
(h * 60 + m) / (hd * 60 + md)

Upvotes: 0

Related Questions