Reputation: 769
I need to run a job/scheduler, which should only update records in database it is falling under the specified time range. In my case, the time range is from 3.30 AM to next day 1.30AM. So, with in this time frame the job needs to update the records. In order to get this time interval, I using TimeOfDay() function, but my logic is getting failed, bcoz if the currenttime is say 6.00 Am, then "currentTime <= todaysJob.ENDTIME.Value.TimeOfDay" is returning false. I am using the below code to check
var currentTime = DateTime.Now.TimeOfDay;
if (currentTime > todaysJob.STARTTIME.Value.TimeOfDay &&
currentTime <= todaysJob.ENDTIME.Value.TimeOfDay)
{
// Do logic
}
Upvotes: 3
Views: 2597
Reputation:
You can use the Time Period Library for .NET, to determine, if a moment falls into multiple time periods:
// ----------------------------------------------------------------------
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
Upvotes: 1
Reputation: 18534
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
}
EDIT
So I added:
Start and End are equal to todaysJob.STARTTIME and todaysJob.ENDTIME, so you take them from DB as they are.
EDIT
If you could have your job like this:
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;
}
}
then you could
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
}
EDIT
I've edited my code another time: added a method to avoid problems with the DayOfWeek enum
Upvotes: 1
Reputation: 3686
You cannot use TimeOfDay property as it is the absolute distance(elapsed time) from a midpoint(Midnight). What is wrong with using just DateTime for comparison? Comparison operators( < > <= >=) work perfectly fine with DateTime datatype.
For Eg:
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
}
Upvotes: 0
Reputation: 1504
Try this, its working for me. Correct me if i'm wrong.
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.
}
Upvotes: 0