174

使用 .NET 3.5

我想确定当前时间是否在一个时间范围内。

到目前为止,我有当前时间:

DateTime currentTime = new DateTime();
currentTime.TimeOfDay;

我正在讨论如何转换和比较时间范围。这行得通吗?

if (Convert.ToDateTime("11:59") <= currentTime.TimeOfDay 
    && Convert.ToDateTime("13:01") >= currentTime.TimeOfDay)
{
   //match found
}

UPDATE1:感谢大家的建议。我不熟悉 TimeSpan 功能。

4

11 回答 11

288

检查一天中的某个时间使用:

TimeSpan start = new TimeSpan(10, 0, 0); //10 o'clock
TimeSpan end = new TimeSpan(12, 0, 0); //12 o'clock
TimeSpan now = DateTime.Now.TimeOfDay;

if ((now > start) && (now < end))
{
   //match found
}

对于绝对时间,请使用:

DateTime start = new DateTime(2009, 12, 9, 10, 0, 0)); //10 o'clock
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0)); //12 o'clock
DateTime now = DateTime.Now;

if ((now > start) && (now < end))
{
   //match found
}
于 2009-10-01T15:08:08.410 回答
143

这里有一些很好的答案,但没有一个涵盖您的开始时间与结束时间不同的情况。如果您需要跨越一天的界限,那么这样的事情可能会有所帮助:

TimeSpan start = TimeSpan.Parse("22:00"); // 10 PM
TimeSpan end = TimeSpan.Parse("02:00");   // 2 AM
TimeSpan now = DateTime.Now.TimeOfDay;

if (start <= end)
{
    // start and stop times are in the same day
    if (now >= start && now <= end)
    {
        // current time is between start and stop
    }
}
else
{
    // start and stop times are in different days
    if (now >= start || now <= end)
    {
       // current time is between start and stop
    }
}

start请注意,在此示例中,时间边界包含在内,并且仍然假设和之间的差异小于 24 小时stop

于 2014-01-24T22:11:10.883 回答
22

一个简单的小扩展功能:

public static bool IsBetween(this DateTime now, TimeSpan start, TimeSpan end)
{
    var time = now.TimeOfDay;
    // Scenario 1: If the start time and the end time are in the same day.
    if (start <= end)
        return time >= start && time <= end;
    // Scenario 2: The start time and end time is on different days.
    return time >= start || time <= end;
}
于 2016-11-11T09:50:22.713 回答
17
if (new TimeSpan(11,59,0) <= currentTime.TimeOfDay && new TimeSpan(13,01,0) >=  currentTime.TimeOfDay)
{
   //match found
}

如果您真的想将字符串解析为 TimeSpan,则可以使用:

    TimeSpan start = TimeSpan.Parse("11:59");
    TimeSpan end = TimeSpan.Parse("13:01");
于 2009-10-01T15:08:02.303 回答
16

尝试在 C# 中使用 TimeRange 对象来完成您的目标。

TimeRange timeRange = new TimeRange();
timeRange = TimeRange.Parse("13:00-14:00");

bool IsNowInTheRange = timeRange.IsIn(DateTime.Now.TimeOfDay);
Console.Write(IsNowInTheRange);

这是我得到使用 TimeRange 示例的地方

于 2009-10-01T15:15:27.183 回答
7

TimeOfDay属性返回一个TimeSpan值。

试试下面的代码:

TimeSpan time = DateTime.Now.TimeOfDay;

if (time > new TimeSpan(11, 59, 00)        //Hours, Minutes, Seconds
 && time < new TimeSpan(13, 01, 00)) {
    //match found
}

此外,new DateTime()与 相同DateTime.MinValue并且将始终等于1/1/0001 12:00:00 AM。(值类型不能有非空默认值)你想使用DateTime.Now.

于 2009-10-01T15:12:49.090 回答
2

您非常接近,问题是您正在将 DateTime 与 TimeOfDay 进行比较。您需要做的是将 .TimeOfDay 属性添加到 Convert.ToDateTime() 函数的末尾。

于 2009-10-01T15:09:54.343 回答
1

这对于处理日边界情况会更简单吗?:)

TimeSpan start = TimeSpan.Parse("22:00");  // 10 PM
TimeSpan end = TimeSpan.Parse("02:00");    // 2 AM
TimeSpan now = DateTime.Now.TimeOfDay;

bool bMatched = now.TimeOfDay >= start.TimeOfDay &&
                now.TimeOfDay < end.TimeOfDay;
// Handle the boundary case of switching the day across mid-night
if (end < start)
    bMatched = !bMatched;

if(bMatched)
{
    // match found, current time is between start and end
}
else
{
    // otherwise ... 
}
于 2016-10-27T02:12:58.737 回答
0

我们不希望我们的服务在夜间运行。所以我们创建了这个条件来检查当前时间在上午 9 点(今天)和 1 点(第二天)的时间窗口内:

if (DateTime.Now > DateTime.Now.Date && DateTime.Now <= DateTime.Now.Date.AddHours(1.00) || 
DateTime.Now >= DateTime.Now.Date.AddHours(9))
{
 // Current Time is within specified time window.
}
于 2021-09-11T15:10:10.067 回答
-1

使用 Linq,我们可以通过这个来简化它

 Enumerable.Range(0, (int)(to - from).TotalHours + 1)
            .Select(i => from.AddHours(i)).Where(date => date.TimeOfDay >= new TimeSpan(8, 0, 0) && date.TimeOfDay <= new TimeSpan(18, 0, 0))
于 2016-07-27T09:32:17.443 回答
-2
 using System;

 public class Program
 {
    public static void Main()
    {
        TimeSpan t=new TimeSpan(20,00,00);//Time to check

        TimeSpan start = new TimeSpan(20, 0, 0); //8 o'clock evening

        TimeSpan end = new TimeSpan(08, 0, 0); //8 o'clock Morning

        if ((start>=end && (t<end ||t>=start))||(start<end && (t>=start && t<end)))
        {
           Console.WriteLine("Mached");
        }
        else
        {
            Console.WriteLine("Not Mached");
        }

    }
 }
于 2019-08-01T10:33:21.177 回答