Essentially you are trying to determine whether a given time range falls between a fixed time range. This sounds like homework so I'm not going to give you the full solution, but hopefully this will be enough to get you going.
You need to convert each time into a
timespan
-
reference[
^] i.e. you will end up with 4 timespan variables which I will call
bottomOfRange, topOfRange, bottomOfTarget
and
topOfTarget
First create yourself a helper function that will compare just one timespan with the fixed range ...e.g.
bool isTimeBetween(TimeSpan InTime, TimeSpan bottomOfTarget, TimeSpan topOfTarget)
{
if (bottomOfTarget> topOfTarget)
throw new Exception("Start of range must be before end of range");
if (InTime < bottomOfTarget|| InTime > topOfTarget)
return false;
else
return true;
}
Then you can call that from another function that takes both of the timespans
bool isRangeInRange(TimeSpan bottomOfRange, TimeSpan topOfRange, TimeSpan bottomOfTarget, TimeSpan topOfTarget)
{
}
Then you call the second function e.g.
if (isRangeInRange(bottomOfRange, topOfRange, bottomOfTarget, topOfTarget))
MessageBox.Show("values are in range");
else
MessageBox.Show("values are not in range");
You've mentioned using a
datetime
but elsewhere are just using times, however to convert the time part of a datetime to a timespan you could use
DateTime dt1 = DateTime.Now;
bottomOfRange = new TimeSpan(dt1.Hour, dt1.Minute, dt1.Second);