473,569 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Compare 2 TimeSpans

This little puzzle has been bugging me for some time. I'm trying to
create a little service that will check a time frame and compare it to
another timeframe (start / stop time). A real-world example is as
follows:

Patsy would like to volunteer time at a booth this year at the fair.
Her optimal timeframe to volunteer is between 1:00pm and 5:00pm and she
inputs this via some form. Let's say that I want to compare this to a
Dictionary<Date Time, DateTimethat stores start-times and end-times.
So I could get back available timeframes such as:

12:00pm, 1:00pm
1:00pm, 2:30pm
2:30pm, 3:30pm
3:30pm, 4:00pm
4:30pm, 5:30pm

The first timeframe obviously won't work because it starts at 12:00pm.
The last timeframe won't work because while it starts at 4:30pm it's not
over until 5:30pm.

I know that TimeSpan is a span of time between 2 times. Here I actually
have 4 times. What is the quickest way (if any) to make sure the first
timeframe satisfies one or more of the other timeframes. There has got
to be a simple way to compare timeframes in the .NET Framework. I just
haven't stumbled upon it.

Any help is greatly appreciated.

Thanks in advance,

Will

*** Sent via Developersdex http://www.developersdex.com ***
Oct 18 '07 #1
1 4095
Will wrote:
This little puzzle has been bugging me for some time. I'm trying to
create a little service that will check a time frame and compare it to
another timeframe (start / stop time). A real-world example is as
follows:

Patsy would like to volunteer time at a booth this year at the fair.
Her optimal timeframe to volunteer is between 1:00pm and 5:00pm and she
inputs this via some form. Let's say that I want to compare this to a
Dictionary<Date Time, DateTimethat stores start-times and end-times.
So I could get back available timeframes such as:

12:00pm, 1:00pm
1:00pm, 2:30pm
2:30pm, 3:30pm
3:30pm, 4:00pm
4:30pm, 5:30pm

The first timeframe obviously won't work because it starts at 12:00pm.
The last timeframe won't work because while it starts at 4:30pm it's not
over until 5:30pm.

I know that TimeSpan is a span of time between 2 times. Here I actually
Semantics maybe, but TimeSpan is actually just a measurement of time. It
doesn't have to be between two particular points, it represents it
generically. IE: If I initialize a TimeSpan to 2 hours, 31 minutes, I'm
really just talking about it being 2 hours, 31 minutes long, not
specifically the 2 hours, 31 minutes that occur between 8:00 AM and
10:31 AM.
have 4 times. What is the quickest way (if any) to make sure the first
timeframe satisfies one or more of the other timeframes. There has got
to be a simple way to compare timeframes in the .NET Framework. I just
haven't stumbled upon it.
All you're really doing is comparing the DateTimes. That's going to be
fast even if you do it a lot.
- Does the current range start before our allowed start?
- Does the current range end before our allowed start?
- Does the current range start after our allowed end?
- Does the current range end after our allowed end?

Pretty textbook to me, what's the problem you're having?

Chris.
Oct 18 '07 #2

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

Similar topics

2
1410
by: BillG | last post by:
I have a datagrid with a start and an end time. I subtract the start time from the end time in each row and place the difference into a TimeSpan variable. Now I want to total up the Timespans and get the total number of hours taken. Dim HourSum as Decimal = 0.0 Later in a method I call HoursSum += Convert.ToDecimal(Hours) **error -...
13
20581
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte array to another? This is my current code // Check for a match
8
7122
by: Vincent | last post by:
has any one seen a program to compare mdbs'. I have ran into a few of them, but none seem to really do that job. Basically what I need to do is, take 2 access mdb's and check the differences between the 2. i am talking about tables, forms, queries, the whole ball of wax. Most of the programs jus do tables, that is the easy part. Also...
11
9691
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
1
3503
by: Linda | last post by:
Hi, Is there a way to do a "text" (rather than "binary") compareison with the "like" operator, without changing the global "Option Compare" setting? I don't want to risk breaking many, many lines of functional code just to get one "like" operation to behave as I wish. I want to check whether a single-character string is (a letter or...
17
4504
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are selected into 2 different SP variables and compared for equal. They are both NULL, but do not compare as equal. When the Not NULL columns (SALARY)...
5
3175
by: antani | last post by:
I need to implement a function with a argument that is a compare function. This compare function must be several for every necessity. For example , I would like a compare function to analyze element of list that are even or a compare function to analyze odd element. example: void f (compare )
26
7115
by: neha_chhatre | last post by:
can anybody tell me how to compare two float values say for example t and check are two variables declared float how to compare t and check please help me as soon as possible
1
2474
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.