3

我需要运行一个作业/调度程序,它应该只更新它在指定时间范围内的数据库中的记录。就我而言,时间范围是从凌晨 3.30 到第二天凌晨 1.30。因此,在这个时间范围内,作业需要更新记录。为了获得这个时间间隔,我使用了 TimeOfDay() 函数,但是我的逻辑失败了,如果当前时间是早上 6 点,那么“currentTime <= todaysJob.ENDTIME.Value.TimeOfDay”将返回 false。我正在使用下面的代码来检查

     var currentTime = DateTime.Now.TimeOfDay;
     if (currentTime > todaysJob.STARTTIME.Value.TimeOfDay && 
          currentTime <= todaysJob.ENDTIME.Value.TimeOfDay)
            {
                // Do logic
            }
4

4 回答 4

1
        bool endTomorrow = true;

        DateTime taskDate = new DateTime(2012, 08, 31);

        TimeSpan Start = new TimeSpan(03, 30, 00);
        TimeSpan End = new TimeSpan(01, 30, 00);

        DateTime currentTime = DateTime.Now;

        bool flag = false;

        if (currentTime.TimeOfDay >= Start)
        {
            if (endTomorrow)
            {
                flag = currentTime.Date <= taskDate || (currentTime.Date == taskDate.AddDays(1) && currentTime.TimeOfDay < End);
            }
            else
            {
                flag = currentTime.TimeOfDay < End;
            }
        }

        if (flag)
        {
            //do the task
        }

编辑

所以我补充说:

  1. 一个布尔标志,确定任务是否应该在第二天结束
  2. 表示任务日期的日期时间变量 (taskDate)

Start 和 End 等于 todaysJob.STARTTIME 和 todaysJob.ENDTIME,因此您可以照原样从 DB 中获取它们。

编辑

如果你能有这样的工作:

public class Job
{
    public TimeSpan STARTTIME;
    public TimeSpan ENDTIME;
    public DayOfWeek taskDayOfWeek;
    public bool IsEndingTommorow;

    public bool IsTomorrow(DayOfWeek d)
    {
        if (d == DayOfWeek.Sunday)
            return taskDayOfWeek == DayOfWeek.Saturday;
        else
            return d <= taskDayOfWeek;
    }
}

那么你可以

        DateTime currentTime = DateTime.Now;

        bool flag = false;

        if (currentTime.TimeOfDay >= todaysJob.STARTTIME)
        {
            if (todaysJob.IsEndingTommorow)
            {
                flag = currentTime.DayOfWeek == todaysJob.taskDayOfWeek || (todaysJob.IsTomorrow(currentTime.DayOfWeek) && currentTime.TimeOfDay < todaysJob.ENDTIME);
            }
            else
            {
                flag = currentTime.TimeOfDay < todaysJob.ENDTIME;
            }
        }

        if (flag)
        {
            //do the task
        }

编辑

我再次编辑了我的代码:添加了一种方法来避免 DayOfWeek 枚举出现问题

于 2012-08-31T04:56:59.973 回答
1

您可以使用.NET 的时间段库来确定某个时刻是否属于多个时间段:

// ----------------------------------------------------------------------
public bool CheckDateBetweenDatesSample()
{
  DateTime now = DateTime.Now;
  TimePeriodCollection periods = new TimePeriodCollection();
  // read periods (Start/end) from database
  // ...
  periods.Add( new TimeRange( start, end ) );
  return periods.HasIntersectionPeriods( now );
} // CheckDateBetweenDatesSample
于 2012-08-31T08:31:10.447 回答
0

试试这个,它对我有用。如我错了请纠正我。

TimeSpan timeOfDay = DateTime.Now.TimeOfDay;
TimeSpan startTimeToD = startTime.TimeOfDay;
TimeSpan endTimeToD = endTime.TimeOfDay;

if (timeOfDay > startTimeToD || timeOfDay < endTimeToD )
{
      Console.WriteLine("Hello World");
}

timeOfDay = new TimeSpan(2, 30, 00); //testcase
if (timeOfDay > startTimeToD || timeOfDay < endTimeToD )
{
     //will never execute.
}
于 2012-08-31T04:50:19.303 回答
0

您不能使用 TimeOfDay 属性,因为它是距中点(午夜)的绝对距离(经过的时间)。仅使用 DateTime 进行比较有什么问题?比较运算符(< > <= >=)与 DateTime 数据类型完美配合。

例如:

DateTime currentTime = DateTime.Now;
DateTime startTime = DateTime.AddMinutes(-50D); //in your case this would be a DT todaysJob.STARTTIME.Value
DateTime endTime = DateTime.AddMinutes(50);// in your case this would be a DT todaysJob.ENDTIME.Value

if(currentTime > startTime && currentTime <= endTime)
{
  Console.Write("Works Fine"); //your logic
}
于 2012-08-31T05:28:23.650 回答