TimeSpan Question

  • Thread starter Thread starter james
  • Start date Start date
J

james

Is it possible given two TimeSpans to obtain the intersection of the two?
For example lets say I have span1 that spans Monday though Firday of this
week and span2 that spans all of Tuesday, then the intersection would be a
span of Tuesday. If they do not intersect at all then an empty span is
returned. Can this already be done with the TimeSpan class as it exists?

Thanks,

JIM
 
Jim,

The TimeSpan structure doesn't have a concept of what particular points
in time it is spanning, just the amount of time between two points in time.

In order to do what you want, you would have to take the two beginning
points, and then take the min of those. Then take the two end points, and
take the max of those, and then take the difference between the two. Or,
just take the min of all four points in time, and the max, and take the
difference of that, they will both give you the same results.

The only thing is you might have to do some logic to make sure that they
do intersect (the beginning or end of one section of time falls in between
the other).

Hope this helps.
 
james said:
Is it possible given two TimeSpans to obtain the intersection of the two?
For example lets say I have span1 that spans Monday though Firday of this
week and span2 that spans all of Tuesday, then the intersection would be a
span of Tuesday. If they do not intersect at all then an empty span is
returned. Can this already be done with the TimeSpan class as it exists?

TimeSpans don't work that way - they don't have starts and ends, just
lengths.

It strikes me that you're after a pair of start/end DateTimes - finding
the intersection of those wouldn't be too hard.
 
Thanks for the answer.

Yes I know how to do it the long way. I just thought it would be really
cool to be able to do an intersection of two date ranges using TimeSpans.

JIM


Nicholas Paldino said:
Jim,

The TimeSpan structure doesn't have a concept of what particular points
in time it is spanning, just the amount of time between two points in
time.

In order to do what you want, you would have to take the two beginning
points, and then take the min of those. Then take the two end points, and
take the max of those, and then take the difference between the two. Or,
just take the min of all four points in time, and the max, and take the
difference of that, they will both give you the same results.

The only thing is you might have to do some logic to make sure that
they do intersect (the beginning or end of one section of time falls in
between the other).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

james said:
Is it possible given two TimeSpans to obtain the intersection of the two?
For example lets say I have span1 that spans Monday though Firday of this
week and span2 that spans all of Tuesday, then the intersection would be
a span of Tuesday. If they do not intersect at all then an empty span is
returned. Can this already be done with the TimeSpan class as it exists?

Thanks,

JIM
 
Back
Top