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

M

Mathias Weyel

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
 
C

Chris Shepherd

Mathias said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top