Heba
Heba

Reputation: 1

How can I ckeck that the current time is within a specific time range?

I need to compare that the current time is within a specific range. I've tried the following

DateTime currentTime = DateTime.Now; 
int Comaprestart = DateTime.Compare(currentTime, startTime);
int CompareEnd = DateTime.Compare(currentTime,endTime);
if ((Comaprestart >= 0) && (CompareEnd <= 0))
     LinkBtnQuiz.Enabled = true;

It doesn't work right. for example I have the end time equal '2014-06-08 13:30:00.000' and the current time equal '2014-06-08 13:12:00.000' but I get CompareEnd=1 while it should be -1 as the current time is earlier than the end time.

Any idea of what is causing the error??

Upvotes: 0

Views: 86

Answers (2)

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

return dateToCheck >= startDate && dateToCheck < endDate;

Check this answer How to know if a DateTime is between a DateRange in C#

Upvotes: 0

Dmitry Khryukin
Dmitry Khryukin

Reputation: 6438

you can do it easier:

if(currentTime >= startTime && currentTime <= endTime)
{
   LinkBtnQuiz.Enabled = true;
}

Upvotes: 1

Related Questions