检查在一个范围内的时间



我试图执行检查是在指定的时间间隔内输入时间,但我卡住了…我有一个主块和另一个执行检查的类是在间隔内输入的时间,但我一直得到"没有重载方法'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);
    }
}

错误提示您正在调用IsBetween:

bool b = now.IsBetween();

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

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

相关内容

  • 没有找到相关文章

最新更新