----EDIT----
Oh im sorry that was a huge misstake :)
Let me ask the question again...
Heres an example,
Datetime startTime = 2011-10-08 12:30:00
Da开发者_如何学JAVAtetime endTime = 2011-10-10 15:00:00
How many times does 12:00:00 - 13:00:00 occur between the two datetimes?
2011-10-08 12:30:00 - 2011-10-08 13:00:00 Not Ok (time has alredy started)
2011-10-09 12:00:00 - 2011-10-09 13:00:00 Ok
2011-10-10 12:00:00 - 2011-10-10 13:00:00 Ok
Expecting result 2.
Thanks in advance!
(endTime-startTime).Ticks/timeSpan.Ticks
After seeing your update you will want to do something like determine the days to check and do a bounds check to see if the times specific fall within your start and end dates. As an idea, here is some sample code I threw together.
private static void CheckTimes()
{
DateTime start = DateTime.Parse("2011-10-08 12:30:00");
DateTime end = DateTime.Parse("2011-10-10 15:00:00");
// variable to use for bound checking (Date property sets the hour to 00)
DateTime boundscheck = start.Date;
// variable containing results
int timesFound = 0;
// This loop assumes we are only looking for one match per day
for (int i = 0; i <= (end - start).Days; i++)
{
// set the lower bound to yyyy-mm-dd 12:00:00
var lowerbound = boundscheck.Date.AddHours(12);
// set the upper bound to yyyy-mm-dd 13:00:00
var upperbound = lowerbound.AddHours(1);
//determine if bounds are within our start and end date
if (lowerbound >= start && upperbound <= end)
{
timesFound++;
}
// increment boundscheck variable by one day
boundscheck = boundscheck.AddDays(1);
}
}
Hope this helps.
Take the difference of the two days, divided by the interval.
TimeSpan timeSpan = new TimeSpan(24, 00, 00); // one day
DateTime start = new DateTime(2011, 10, 08, 11, 00, 00);
DateTime end = new DateTime(2011, 10, 10, 23, 00, 00); // 2 and 1/2 days later
var occurances = ((end - start).Ticks / (float)timeSpan.Ticks); // 2.5f
精彩评论