Click here to Skip to main content
15,908,843 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I give my code snipple down part.My problem is when i run this code i get false all time. How can i fix this?

C#
While(!isStop)
{
	var isReportOnly = false;
	var isInReportPeriod = false;

	DateTime dtStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0);
	DateTime dtStop = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 06, 0, 0);
	if (DateTime.Now > dtStart && DateTime.Now < dtStop)
	{
		isInReportPeriod = true;
		log.Add( DateTime.Now.ToString() + "=True\n");
	}
	else
	{
		isInReportPeriod = false;
		log.Add(DateTime.Now.ToString() + "=False\n");
	}
}


What I have tried:

How can i fix the problem for time comprison in the loop
Posted
Updated 11-Jun-17 20:25pm

1 solution

Um.
Start is 22:00, stop is 06:00.
To log TRUE, you need the current time to be both after 22:00 and before 06:00 on the same day...

Possibly you got the start and stop times wrong?

But don't do it like that anyway: never read the time more than once, it can cause problems when the day, (or worse month or year) changes.

C#
DateTime now = DatetIme.Now;
DateTime start = now.Date.AddHours(6);
DateTime stop = now.Date.AddHours(22);
if (now > start && now < stop)
   {
   ...
 
Share this answer
 

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