471,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

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 8535
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Lei Guangfu | last post: by
1 post views Thread by Lei Guangfu | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.