检查是一个范围内的时间

时间:2014-03-27 21:13:19

标签: c# datetime timeofday

我尝试执行检查是在指定的时间间隔内输入的时间但是我卡住了...我有一个主要的块,另一个执行检查的类是在间隔内输入的时间但是我一直得到"否方法过载' IsBetween'取0个参数"当我尝试调用该方法并获得简单的bool true或false时,我知道解决方案可能非常简单,但我无法弄清楚......:

 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);
    }

这是我的班级检查:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

错误表明您正在调用IsBetween,如:

bool b = now.IsBetween();

但您需要提供开始和结束时间:

bool b = now.IsBetween(startTime, endTime);