Reaching out – Giving back – Looking inward – Paying forward
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.
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.
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.
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.
Only one word Awsome :)
ReplyDeleteBrilliant Explanation :)
ReplyDeleteperfect!!!
ReplyDeleteExcellent!
ReplyDeleteWhat's your paypal so I can buy you a case of microbrew? :D
ReplyDeleteWow. Excellent explanation! Thanks so muuch!
ReplyDeleteGood one
ReplyDeleteYou saved my day, thanks!
ReplyDeleteI found that you need only to make a simple calculation:
ReplyDeleteIf (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
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