Some good answers here but none cover the case of your start time being in a different day than your end time. If you need to straddle the day boundary, then something like this may help:
TimeSpan start = TimeSpan.Parse("22:00"); // 10 PM
TimeSpan end = TimeSpan.Parse("02:00"); // 2 AM
TimeSpan now = DateTime.Now.TimeOfDay;
if (start <= end)
{
// start and stop times are in the same day
if (now >= start && now <= end)
{
// current time is between start and stop
}
}
else
{
// start and stop times are in different days
if (now >= start || now <= end)
{
// current time is between start and stop
}
}
Note that in this example the time boundaries are inclusive and that this still assumes less than a 24-hour difference between start
and stop
.