Bring back the truth When does time fall between the time range?

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
}
+3
source share
5 answers

Divide one check, if it covers the middle, and one for the same day.

TimeSpan start = new TimeSpan(starthour, startminute, 0); 
TimeSpan end = new TimeSpan(endhour, endminute, 0);
TimeSpan now = DateTime.Now.TimeOfDay;

//The readable version:
if(start>end){
  //Must check if after start (before midnight) or before end (after midnight)
  if((now > start) || (now < end)){
    return true;
  {
}
else
{
  //Simple check - span is within same day
  if ((now > start) && (now < end))
  {
    return true;
  }
}
return false;

Short / mysterious option:

return start > end ? (now > start) || (now < end) : (now > start) && (now < end);
+5
source

DateTime, . DateTimes. secondTime firstTime, 1 secondTime.

public bool IsBetween(this DateTime thisTime, DateTime firstTime, DateTime secondTime) {
    if (secondTime < firstTime)
        secondTime = secondTime.AddDays(1);
    return firstTime < thisTime && thisTime < secondTime);
}

// to use...
bool isDoTime = DateTime.Now.IsBetween(firstTime, secondTime);
+3

, DateTime, , , , 24 .

:

bool isDoTime(int starthour, int startminute, int endhour, int endminute)
{
    DateTime start = new DateTime(0, 0, 0, starthour, startminute, 0); 
    DateTime end = new DateTime(0, 0, 0, endhour, endminute, 0);
    if (start > end)
    {
        end.AddDays(1);
    }
    DateTime now = DateTime.Now;

    return start < now && now < end;
}

<=

( , 24 , , ?)

+1

DateTime :

bool isDoTime(DateTime starttime, DateTime endtime)
{
  if (DateTime.Now > starttime && DateTime.Now < endtime)
    {
        return true;
    }
    return false;
}
0
[TestFixture]
    public class Class1
    {
        private DateTime _now;

        [SetUp]
        public void SetUp()
        {
            _now = DateTime.MinValue.AddDays(1).AddHours(2); //02.01.0001 02:00:00
        }

        [Test]
        public void TestCase()
        {
            Assert.IsTrue(IsDoTime(20, 30, 3, 30));
        }

        bool IsDoTime(int starthour, int startminute, int endhour, int endminute)
        {
            var start1 = DateTime.MinValue.AddHours(starthour).AddMinutes(startminute); //01.01.0001 20:30:00

            var end1 = endhour < starthour
                           ? DateTime.MinValue.AddDays(1).AddHours(endhour).AddMinutes(endminute) //02.01.0001 03:30:00
                           : DateTime.MinValue.AddHours(endhour).AddMinutes(endminute);

            return ((_now > start1) && (_now < end1));
        }
    }
0
source

Source: https://habr.com/ru/post/1791195/


All Articles