I am making a function to check the time between the time interval in 24-hour format. However, there is something wrong with my code, can someone please indicate how to fix it?
My code is:
bool isDoTime(int starthour, int startminute, int endhour, int endminute)
{
TimeSpan start = new TimeSpan(starthour, startminute, 0);
TimeSpan end = new TimeSpan(endhour, endminute, 0);
TimeSpan add24h = new TimeSpan(24, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
if (starthour > endhour || (endhour == starthour && endminute <= startminute))
{
end += add24h;
}
if ((now > start) && (now < end))
{
return true;
}
return false;
}
Problem: I want to return true when the current time is between 20:30 - 3:30, however, when I run my code as shown below. the condition returns true only from 8:30 to 00:00, and not from 00:00 to 3:30
if (isDoTime(20,30,3,30) //return true from 20:30 - 3:30
{
//dosomething
}
source
share