Thursday, June 5, 2014

Date Range Overlap

The best way to figure out if two date ranges overlap is explained in this stackoverflow answer. However, there is additional information below on calculating the actual amount of overlap, if any.

10 comments:

  1. What's your paypal so I can buy you a case of microbrew? :D

    ReplyDelete
  2. Wow. Excellent explanation! Thanks so muuch!

    ReplyDelete
  3. I found that you need only to make a simple calculation:

    If (B + D) >= (A + C), then you have overlap. In words, if the sum of the endings if greater of equal to the sum of the beginnings, you have overlap.

    But, if you do that four calculations, B-A, D-C, B-C, D-A, you find the size of the overlap by taking the least and if that least is greater than zero. I mean, if the least of the four calculations is zero or negative, automatically you have no overlaps, so you do not need to make that first calculation.

    Or

    ReplyDelete
  4. The easiest calculation seems to be: calculate min(B,D) - max(A,C). That's your overlap. If it's negative, there is no overlap. If it's zero, the periods are adjacent or one of them has zero length.

    ReplyDelete

Thank you for your time and interest in this post!
Comments to this blog are sometimes moderated to prevent spam. Please don't be alarmed if your comment does not appear immediately.