.NET Framework - How to Compare 2 TimeSpans

Asked By Will
17-Oct-07 11:23 PM
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<DateTime, DateTime> that 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 ***
DateTime
(1)
TimeSpan
(1)
Intersection
(1)
Patsy
(1)
Containment
(1)
Timeframe
(1)
Timetable
(1)
Struct
(1)
  Marc Gravell replied...
18-Oct-07 12:17 AM
I had a similar problem while working on some scheduling (timetable-
style) software a few years back. My solution was to create an
Interval struct that comprised 2 DateTime values, with methods to
check the Intersection between 2 Intervals, get the Duration, or check
containment (with bool parameter to indicate whether matching bound
were allowed, or if it was strict). I also (for interest) set them up
so that sort and equality were defined in terms of both dates in
order.

Let me know if this doesn't help you ;-p I've spent many an hour in
this exact area...

Marc
  Marc Gravell replied...
18-Oct-07 12:18 AM
I had a similar problem while working on some scheduling (timetable-
style) software a few years back. My solution was to create an
Interval struct that comprised 2 DateTime values, with methods to
check the Intersection between 2 Intervals, get the Duration, or check
containment (with bool parameter to indicate whether matching bound
were allowed, or if it was strict). I also (for interest) set them up
so that sort and equality were defined in terms of both dates in
order.

Let me know if this doesn't help you ;-p I've spent many an hour in
this exact area...

Marc
  Chris Shepherd replied...
18-Oct-07 08:48 AM
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.


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.
Create New Account
help
DATETIME to only return DATE .NET Framework What magic to I need to do to get the DATETIME.Today to only return the DATE?!? C# Discussions ToShortDateString (1) ToLongDateString (1) TimeSpan (1) DateTime (1) Class (1) Date (1) String Felix Palmen (1) Felix Felix Palmen (1) Do you mean the "System.DateTime" struct? There is no such type as For System.DateTime, it is not possible for it "to only return the date". Note the name of the type: "DateTime". it is a date _and_ a time. Always. If you want to only _display_ the date after you have retrieved a particular DateTime value (including DateTime.Today), then just do that. There is nothing forcing you to include
DateTime.MinValue.Year gives 2001 when parsed? .NET Framework Hello, I tried the following: String dts = String.Format("{0}-{1}-{2}", DateTime.MinValue.Day, DateTime.MinValue.Month, DateTime.MinValue.Year); DateTime dt = DateTime.Parse(dts); dts becomes "1-1-1" but dt becomes "01-01-2001". Shouldn't the minimum year be "0001" which is what I get when i do DateTime dt = new DateTime(); Thanks, Miguel C# Discussions Exception (1) DateTime (1) Decimal (1) Class (1) ParseExact (1) Console
convert UTCDateTime to Hexidecimal and back? .NET Framework I want to convert a simple UTC DateTime value (e.g. 2009-11-20 3:32:00 PM) to a hexidecimal value or code for this? C# Discussions System.Globalization.NumberStyles.HexNumber (1) MConvertBase64StringToUTCDateTime (1) BitConverter.IsLittleEndian (1) DateTime.TotalSeconds (1) BitConverter.GetBytes (1) BitConverter.ToInt32 (1) DateTime.UtcNow (1) FromBase64String (1) How about converting it to a Julian date? Here is some code. http: / / www.dotnetblogger.com / post / 2009 / 02 / 26 / Convert-DateTime-to-Julian-Date-in-C.aspx / / Anders I think the simplest approach would be to just use the DateTime.Ticks value, formatted as hex or Base64 (the latter being slightly more efficient than hex if you only need a smaller range, and / or less precision, you can represent the DateTime in even fewer characters. Hopefully the above gives you the basic idea. If you need change it to Base64 I'd appreciate it. I only need precision to seconds. The datetime values will always be greater than 1 / 1 / 2000. It is for a transaction timestamp code change in a hundred years. . . Here is the code: class cDateMethods { public string mConvertUTCDateTimeToHex(DateTime dtDateTimeIn) { / / Convert DateTime to Hex long decValue = dtDateTimeIn.Ticks; string hexValue = decValue.ToString("X"); return
This is call to show an online timer and it intermittently will show the milliseconds DateTime dtTimer; / / initial time. private void start(object sender, EventArgs e) { dtTimer = DateTime.Now; / / initialize it } private void timer1_Tick(object sender, EventArgs e) { TimeSpan dt2 = DateTime.Now - dtTimer; / / These two will show the ticks. lbTimeOnline.Text = dt2.ToString(); lbTimeOnline.Text = string in kindergarden again relearning all sorts of basic stuff. <g> - - HLS C# Discussions EventArgs (1) TimeSpan (1) DateTime (1) InitialDateTimeNow (1) DateTimeOffSet (1) Format (1) Split (1) Tick (1) dt2.ToString("hh:mm show the milliseconds, it is probably easiest to just get the TotalMilliseconds property of the TimeSpan value, and format that as a single number. I have always wondered why the TimeSpan type does not provide more elaborate formatting options. But as "kndg" says, if you want
System.IO.StringWriter (1) ParametersDECLARE (1) UbVar.ArUnterkunftPreisePauschalpreisBasis (1) ArUnterkunftPreisePauschalpreisBasis (1) StringWriter (1) SqlProvider (1) DateTime (1) Hallo Georg, Ich habe doch Zweifel, dass der Output der richtige ist. Gehe mal Model, falls dort Erweiterungen eingebaut sind. Denn ohne das wird das sehr m?hsam nachzustellen. DateTime wird generell unterst?tzt: http: / / msdn.microsoft.com / de-de / library / bb882657.aspx Gru? Elmar Ich begreife es nicht, aber es greift _NICHT_ = :-( Habe das hier mal getestet: void Main() { DateTime p1 = new DateTime(2010, 01, 01); DateTime p2 = new DateTime(2010, 01, 31); DateTime p3 = new DateTime(2010, 05, 01); DateTime p4 = new DateTime(2010, 05, 04); DateTime p5 = new DateTime(2010