Ranges of time logic?

  • Thread starter Thread starter Justin
  • Start date Start date
J

Justin

I'm trying to write a routine that checks records to see if they are
between a range of times and days.

For example, someone might want records dated from August 2-August 5
from 5:00am to 10:50am. It should only return records from the 2nd to
the 5th, and from 5:00am to 10:50am (nothing before or after those
times).

I have been trying to do this using an hour and a minute integer but I
haven't been able to solve the logic.

Is there an easier way to accomplish this? I can't find much about it
using DateTime either.
 
Justin said:
I'm trying to write a routine that checks records to see if they are
between a range of times and days.

For example, someone might want records dated from August 2-August 5
from 5:00am to 10:50am. It should only return records from the 2nd to
the 5th, and from 5:00am to 10:50am (nothing before or after those
times).

Something like

d.Date >= new DateTime(2008, 8, 2) && d.Date <= new DateTime(2008, 8, 5) &&
d.TimeOfDay >= new TimeSpan(5, 0, 0) && d.TimeOfDay <= new TimeSpan(10, 50, 0)

if I understand you correctly.
 
Try something like this, it will need to be adapted to suit your exact
situation but this should work.


DateTime EarliestDateTime = new DateTime(2008,8,2,5,0,0);
DateTime LatestDateTime = new DateTime(2008,8,5,10,50,0);

List<DateTime> datetimes;
List<DateTime> results= datetimes.FindAll(new
Predicate<DateTime>(delegate(DateTime theDate)
{
return theDate >= EarliestDateTime && theDate <= LatestDateTime &&
theDate.TimeOfDay >= EarliestDateTime.TimeOfDay && theDate.TimeOfDay <=
LatestDateTime.TimeOfDay;
}));
 
Back
Top