C# limit the hour with minute

i want to enable the checkbox only between 12:30 -- 14:00 and 18:00 -- 21:00.

if (((DateTime.Now.Hour >= 12 || DateTime.Now.Minute >= 30) && DateTime.Now.Hour < 14) || (DateTime.Now.Hour >= 18 && DateTime.Now.Hour < 21))
        { ASPxCheckBox_ForceClot.Enabled = true; }

the problem is minute, 12:30

DateTime.Now.Hour >= 12 || DateTime.Now.Minute >= 30

it reject 12:45 but it should take this value

 DateTime.Now.Hour >= 12 && DateTime.Now.Minute >= 30

it reject 13:12 but it should take this value

thanks in advance

Jon Skeet
people
quotationmark

I suggest you do it by actual times of day (represented as TimeSpan values) instead:

var firstPeriodStart = new TimeSpan(12, 30, 0);
var firstPeriodEnd = new TimeSpan(14, 0, 0);

var secondPeriodStart = new TimeSpan(18, 0, 0);
var secondPeriodEnd = new TimeSpan(21, 0, 0);

DateTime time = DateTime.Now.TimeOfDay;

if ((time >= firstPeriodStart && time < firstPeriodEnd) ||
    (time >= secondPeriodStart && time < secondPeriodEnd))
{
    ASPxCheckBox_ForceClot.Enabled = true;
}

Better yet (IMO) use the LocalTime type in Noda Time as that represents what you're really interested in.

You should also bear in mind that the code above uses the local time on the server - if your users are in a different time zone, is that okay?

people

See more on this question at Stackoverflow