确定落在时间范围之间的时间戳

时间:2012-08-31 04:38:49

标签: c# datetime

我需要运行一个job / scheduler,它应该只更新它在指定时间范围内的数据库中的记录。就我而言,时间范围是从凌晨3点30分到次日凌晨1点30分。因此,在此时间范围内,作业需要更新记录。为了获得这个时间间隔,我使用了TimeOfDay()函数,但是我的逻辑失败了,如果当前时间是6.00 Am,则bcoz,然后“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 个答案:

答案 0 :(得分: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)
  3. 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枚举问题的方法

答案 1 :(得分:1)

您可以使用Time Period Library for .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

答案 2 :(得分: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.
}

答案 3 :(得分:0)

您不能使用TimeOfDay属性,因为它是中点(午夜)的绝对距离(经过时间)。使用DateTime进行比较有什么问题?比较运算符(&lt;&gt;&lt; =&gt; =)与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
}