What's the formula?

var startTime = new DateTime(2010, 10, 1, 9, 0, 0);
var endTime = new DateTime(2010, 10, 1, 16, 0, 0);
var interval = 11;

So given a start time of 9am, an end time of 4pm and an interval of 11 how can I find out how many time slots there are between the start and end times?

Does that make sense? I know the answer will be 43 time slots, but how to derive it?

Sorry, I wasn’t even referring to you, I was actually referring to his original example where he stated he wanted 11min intervals.

Yes, sorry, it was a typo. I copied what he gave me and forgot to change the 11 interval to a 10. As he wants 10min intervals as he states above with his example.

Another thing you can do, which I have done in the past is the following:


var startTime = new DateTime(2010, 10, 1, 9, 0, 0);
var endTime = new DateTime(2010, 10, 1, 16, 0, 0);
var interval = 11;

while (startTime <= endTime)
{
    Response.Write(startTime.ToString("HH:mm")); //OR HOWEVER YOU WANT TO DISPLAY IT
    startTime.AddMinutes(interval);
}

I really don’t understand how you get 43 using the figures you specified above, it should be 38 :slight_smile:

Yeah. If you think about it, the first slot would be at position 0, which the normal math wouldn’t give you, which is why you add 1.

okay, so I used this:

System.TimeSpan diffResult = endTime.Subtract(startTime);
var slots = (diffResult.Hours*60) / interval;

which does seem to kind of work.

I also meant to use an interval of 10 which returns 42 results, instead of 43. I guess it doesn’t count the starting point?

if you do this:
9:00am
9:10am
9:20am
9:30am

4:00pm

you should get 43 results… So I guess I just add 1 at the end?

how about finding the date difference, then devide that by the interval?