473,241 Members | 1,475 Online

# Check, if a given time lies between two times regardless of date

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
Jun 27 '08 #1
2 8675
"Mathias Weyel" <ne**@josquin.orgwrote:
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.
Jun 27 '08 #2
Mathias Weyel wrote:
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.
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.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.