Testing Time Values

  • Thread starter Thread starter Jeff B.
  • Start date Start date
J

Jeff B.

Hello,

Has anyone ran across an efficient algorithm for determining if a DateTime
or TimeSpan value falls within two other DateTime/TimeSpan values?

For example, if I have a start time of 09:00 and an end time of 21:00 and I
want to test if 16:00 is between them, this is fairly straightforward.
However, if my start time is 21:00 and my end time is 09:00 and I want to
test if 04:00 is between them, then it's not quite as straightforward.

I don't want to take the actual dates into account, only the times.

Any ideas?

--- Thanks, Jeff
 
Hi Jeff,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to check if a certain DateTime
is in a certain range. If there is any misunderstanding, please feel free
to let me know.

There is no direct method for us to call in the .NET framework. Since it
has no date information in it, I think it's hard to write such a method.
For example, we need to check if a time is between 21:00 and 9:00. Now we
pass in 6:00. But how do we know it is the 6:00 of the first day or the
next day?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
For example, we need to check if a time is between 21:00 and 9:00. Now we
pass in 6:00. But how do we know it is the 6:00 of the first day or the
next day?

Kevin,

As far as my needs go, I'm not concerned about which day it is. In the
example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
which it does, then I would want a result of true to come back. If I were
testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
reverse of the example above) then I would want false to come back since
06:00 does not fall anywhere between 9 in the morning and 9 in the evening.

Does this make more sense?

--- Thanks, Jeff
 
Hi Jeff

Try this. It works for me

// Check if a given TimeSpan DateTime is between two other given TimeSpans

private bool IsTimeBetween(TimeSpan tsToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
bool bRet = false;
if (tsStart > tsEnd)
{
TimeSpan ts0 = new TimeSpan(0); // 00:00:00
TimeSpan ts1 = new TimeSpan(TimeSpan.TicksPerDay-1); // 23:59:59.xxx
bRet = (IsTimeBetween(tsToTest, tsStart, ts1) ||
(IsTimeBetween(tsToTest, ts0, tsEnd)));
}
else
{
if ((tsToTest >= tsStart) && (tsToTest <= tsEnd))
{
bRet = true;
}
}
return bRet;
}

// Check if time part of a given DateTime is between two given TimeSpans
private bool IsTimeBetween(DateTime dtToTest, TimeSpan tsStart, TimeSpan
tsEnd)
{
return IsTimeBetween(dtToTest.TimeOfDay, tsStart, tsEnd);
}

Boaz Ben-Porat
Milstone Systems
 
Jeff B. said:
As far as my needs go, I'm not concerned about which day it is. In the
example you state above, if 06:00 falls "anywhere" between 21:00 and 09:00,
which it does, then I would want a result of true to come back. If I were
testing to see if 06:00 falls "anywhere" between 09:00 and 21:00 (the
reverse of the example above) then I would want false to come back since
06:00 does not fall anywhere between 9 in the morning and 9 in the evening.

Does this make more sense?

Not really, because it falls between 9 in the morning and 9 on the
following evening. I think you need a more precise definition of
exactly what you mean. Once you've got a precise definition, turning it
into code should be simple.
 
Not really, because it falls between 9 in the morning and 9 on the
following evening. I think you need a more precise definition of
exactly what you mean. Once you've got a precise definition, turning it
into code should be simple.

Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
following 09:00. In other words, if the timespan being tested falls between
the starting timespan and the first occurrence of the ending timespan
(whether it happens to fall on the first day or next day) then I want a
method that returns true; else I want it to return false.

I hope this makes more sense.

--- Thanks, Jeff
 
Boaz,

This algorithm works great. It's along the same lines of what I was
attempting but a lot simpler and cleaner. Thanks for the help.

--- Jeff
 
Jeff B. said:
Ok, to be more precise, if I were testing to see if 06:00 falls "anywhere"
between 09:00 and 21:00 I am talking about the _first_ occurrence of 21:00
following 09:00. In other words, if the timespan being tested falls between
the starting timespan and the first occurrence of the ending timespan
(whether it happens to fall on the first day or next day) then I want a
method that returns true; else I want it to return false.

I hope this makes more sense.

Right. In that case, I think you need to:

a) Take the start time and the end time, constructing DateTimes for
both of them on the same day
b) If the end time is currently before the start time, add 24 hours
c) See if the test time is between start time and end time
 
Boaz Ben-Porat said:
Try this. It works for me

<snip>

I think that can be made a bit more readable, with the same interface:

static bool IsTimeBetween (TimeSpan tsToTest,
TimeSpan tsStart,
TimeSpan tsEnd)
{
DateTime day = DateTime.Today;

DateTime start = day+tsStart;
DateTime end = day+tsEnd;
DateTime test = day+tsTest;

if (start > end)
{
end = end.AddDays (1);
}
return (test >= start && test <= end);
}
 
Back
Top