2

Having today's date for example: DateTime.Now

And 2 TimeSpan that represents two periods of time

DateTime mydate = DateTime.Now;

TimeSpan start = TimeSpan.Parse("14:00:00");
TimeSpan end =   TimeSpan.Parse("15:00:00");

// TO DO: 

How to check that mydate time (TimeOfDay) is not between start and end range.

Basically check if the hours, minutes, seconds are between 14:00 and 15:00 or outside this range.

UPDATE:

The right condition is: mydate.TimeOfDay <= start || mydate.TimeOfDay >= end

user2818430
  • 5,853
  • 21
  • 82
  • 148

1 Answers1

4

Comparing them seems to work seams to work. TimeOfDay is a TimeSpan just like start and end

Console.WriteLine(mydate.TimeOfDay <= start || mydate.TimeOfDay >= end);

Fiddle

Bobby Tables
  • 2,953
  • 7
  • 29
  • 53
  • The right condition is: `mydate.TimeOfDay <= start || mydate.TimeOfDay >= end`. I'll mark your answer as correct for sending me on the right direction. – user2818430 Jun 28 '15 at 18:10
  • @user2818430 Thank you initially was look if it was inside the interval when i updated forgot to change && into || – Bobby Tables Jun 28 '15 at 18:15
  • 1
    Usually a range of times is a half open interval `[start,end)`. To test inside the range, use `time >= start && time < end`. To test *outside* the range, use `time < start || time >= end`. You have it close, but are not excluding the start value. – Matt Johnson-Pint Jul 08 '15 at 20:21