473,414 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 8687
"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.

Similar topics

3
by: CrystalDBA | last post by:
I am using SQL Server 2000. I need to query my database for all the contracts that came in during a certain time frame (user is prompted for reportingperiodid). Table - Periods Fields -...
1
by: Lei Guangfu | last post by:
Hi, I have a program need to check the value of a application available, and then decide whether to execute codes followed. In order to achieve this purpose, I use DO WHILE loop to judge the...
1
by: Lei Guangfu | last post by:
Hi, I have a program need to delay for a given time, I use DO WHILE loop to achieve this goal, but this method consumes too much CPU resource. So I want to find a way to pause a process for a...
2
by: Noozer | last post by:
I have a textbox on one of my forms that is used to accept a time from the user. I need to write this value to a database to trigger an event later on. What I'd like to know is... Are there...
7
by: geo039 | last post by:
I have a simple application that takes user input by text and time selected by date time picker. It displays the appt description in one list box and the time in another list box. I need a simple...
3
by: Mark Ingram | last post by:
Hi, I'd like to know the best way of checking the current time during a demonstration product. At the minute i store the first run date, then compare that to the system time, but obviously a user...
13
by: dizzydangler | last post by:
Just a quick question...I'm running an MS Access 2007 db that tracks appointments in a single table. Date and time are entered as separate fields in short date (mm/dd/yyyy) and short time (hh:mm)...
1
by: sasasasa | last post by:
How can I loop through each date in a given time period so that I can add those dates as a column name in a table. I want to create datatable with the person's name and hours he worked in a given...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.