0

Im trying to perform a check is entered time inside specified time interval but Im stuck... I have one main block and another class which perform the check is the entered time inside the interval but I keep getting "No overload for method 'IsBetween' takes 0 arguments" when I try to call the method and get simple bool true or false, I know that solution is probably really simple but I just cant figure it out... :

 static void Main()
    {
        var currentTime = Console.ReadLine();
        DateTime now = Convert.ToDateTime(currentTime);
        var dateNow = DateTime.Now;
        var startTime = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 13, 0, 0);
        var endTime = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, 3, 0, 0);
    }

This is my class for check :

public static class TimeExtensions
{
    public static bool IsBetween(this DateTime now, DateTime startTime, DateTime endTime)
    {
        if (now.TimeOfDay == startTime.TimeOfDay) return true;
        if (now.TimeOfDay == endTime.TimeOfDay) return true;

        if (startTime.TimeOfDay <= endTime.TimeOfDay)
            return (now.TimeOfDay >= startTime.TimeOfDay && now.TimeOfDay <= endTime.TimeOfDay);
        else
            return !(now.TimeOfDay >= endTime.TimeOfDay && now.TimeOfDay <= startTime.TimeOfDay);
    }
}
tslid
  • 315
  • 1
  • 4
  • 16
  • You haven't shown the code where you actually call the `IsBetween` function...! – RB. Mar 27 '14 at 21:14
  • You need to provide the start and end time when you try call `IsBetween`. – Lee Mar 27 '14 at 21:15
  • So `bool test = dateNow.IsBetween(startTime, endTime)` doesn't compile? Have you checked namespaces, etc.? – Ryan Mar 27 '14 at 21:15
  • I think your inverted check in the else clause should be using > and < rather than >= and <= ... then you might not need the extra checks of == up front. Or you could write it as `(now.TimeOfDay >= startTime.TimeOfDay || now.TimeOfDay <= end.TimeOfDay)`--without the ! to invert it--which shows more clearly that it's true on the boundaries as in the then clause. – Rob Parker Mar 27 '14 at 21:58
  • Or replace the entire if with simply `(now.TimeOfDay >= startTime.TimeOfDay ^^ now.TimeOfDay > endTime.TimeOfDay)`. It may also be better to pass a TimeSpan for the start and end rather than DateTime so you can specify 0 and 24:00:00. – Rob Parker Mar 27 '14 at 22:08
  • Thanks! This will make it simpler - I will try it! Lee got it right, I just forgot to add startTime, endTime .... – tslid Mar 27 '14 at 22:08
  • The IsBetween method, you have reversed the start and end to cope with a person putting them in the other way, but you have also ! the result, so that wont work. It will return false when its inbetween and the start and end are reversed. Just in case somebody copies it blindly in a hurry. – julian guppy Feb 12 '20 at 07:17

1 Answers1

0

The error suggests you are calling IsBetween like:

bool b = now.IsBetween();

but you need to provide the start and end times:

bool b = now.IsBetween(startTime, endTime);
Lee
  • 142,018
  • 20
  • 234
  • 287