Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I have an application which deals with closing another application down at certain times of the day.

This has worked fine when the schedule has been set to be within the same day such as 1800hrs till 2100hrs.

Now that our schedule has moved to 2345hrs till 0200hrs so it overlaps into the next day, the application is failing to reach that case statement and thus at 2345hrs is never closing the defined applications.

Any suggestions?

This is the If Statement that handles:
now = current time
startdisallowedtime = 2345hrs
enddisallowedtime = 0200hrs.
C#
((now >= startDisallowedTime) && (now <= endDisallowedTime))
Posted
Updated 2-Aug-12 1:22am
v2

There's two possible situations with the range: either it goes over a day boundary (e.g. 1800-0600) or it doesn't (e.g. 0600-1800). One of them needs a simple range check and one needs the check given by Mehdi, or a check with the next day. You check which you have by comparing the times:

DateTime now = DateTime.Now;
DateTime start = new DateTime(now.Year, now.Month, now.Day, startRangeHour, startRangeMinute, 0),
         end   = new DateTime(now.Year, now.Month, now.Day, endRangeHour, endRangeMinute, 0);

if(end < start) end = end.AddDays(1);

if(now > start && now < end){
 // disallow
}
 
Share this answer
 
Comments
Mehdi Gholam 2-Aug-12 11:00am    
5'ed
Between 23:45 and 2:00 there is 00:00 or midnight
C#
DateTime midnight = DateTime.Now;
midnight.Hour = 23;
midnight.Minute = 59;
midnight.Seconds = 59;

if((now >= startDisallowedTime && now <= midnight) || (now >= midnight && now <= endDisallowedTime)
   // disallow
 
Share this answer
 
Comments
CPallini 2-Aug-12 8:00am    
I think you have to replace 'now >= midnight' with 'now > midnight'.
--Carlo the Nitpick.
BobJanova 2-Aug-12 10:53am    
This is a partial solution, because if you set a range like 18-20h this will always fail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900