C# time bool always returning false -
C# time bool always returning false -
i have bool tells me if current time in between 2 other times go dark @ 8 (20:00) in eve , lite @ 7 in morning.. not sure if should doing 7 or 07 have tried booth still getting false?
can tell me why returning false? not much else it's returning false when in between 2 times currently.. gtm timezone london, thanks!
public static bool nighttime { { timespan span = lastmoodlightupdate - datetime.now; timespan start = new timespan(20, 0, 0); //dark @ 20:00 timespan end = new timespan(07, 0, 0); //light @ 07:00 timespan = datetime.now.timeofday; homecoming ((now > start) && (now < end)); } }
the problem here comparing 2 timespan
values simple numeric comparison.
as such, no time can both larger 20:00 , smaller 07:00 in terms of values. humans can deal such incongruities, computer can't.
you need consider want here:
|---------|..................................|-----------| 00 07 20 24
you want times in dashes, should utilize ||
instead of &&
:
return (now > start) || (now < end);
since time of day cannot negative, nor can reach 24:00 or higher, give want.
c#
Comments
Post a Comment