Hi there,
I have time-of-day spans presented as strings and want to check, if, at
a given time, I am within such a time span.
Example: Timespan-string is given as follows: "09:00 , 16:00"
A function should now return true if the current time of day is between
9:00 and 16:00 and false otherwise.
Any help is appreciated. The problem sounds trivial, yet I currently
fail to come up with a pleasant solution.
cheers,
Mathias
Paul E Collins - 18 Apr 2008 10:35 GMT
> I have time-of-day spans presented as strings and want to check, if,
> at a given time, I am within such a time span.
> Example: Timespan-string is given as follows: "09:00 , 16:00"
> A function should now return true if the current time of day is
> between 9:00 and 16:00 and false otherwise.
string t = "09:00 , 16:00";
string start = t.Substring(0, 5), end = t.Substring(8, 5);
string now = DateTime.Now.ToString("HH:mm");
return (now >= start && now <= end);
Eq.
Chris Shepherd - 18 Apr 2008 13:15 GMT
> Hi there,
>
[quoted text clipped - 8 lines]
> Any help is appreciated. The problem sounds trivial, yet I currently
> fail to come up with a pleasant solution.
It is fairly trivial: just parse out the actual TimeSpans you want out of
timespan-string so that you have two strings: one containing "09:00" and another
containing "16:00". Create TimeSpans with them them (TimeSpan.Parse), and return
whether DateTime.Now.TimeOfDay is between them.
Chris.