Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Last active April 5, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mxriverlynn/77d2da52f0548f83f127 to your computer and use it in GitHub Desktop.
Save mxriverlynn/77d2da52f0548f83f127 to your computer and use it in GitHub Desktop.
checking date overlaps
{
start: new Date("January 2, 2003"),
end: new Date("March 12, 2006")
}
var sortedRanges = dateRanges.sort((previous, current) => {
// get the start date from previous and current
var previousTime = previous.start.getTime();
var currentTime = current.start.getTime();
// if the previous is earlier than the current
if (previousTime < currentTime) {
return -1;
}
// if the previous time is the same as the current time
if (previousTime === currentTime) {
return 0;
}
// if the previous time is later than the current time
return 1;
});
// start at index of 1 so there will
// always be a previous range to use
var length = dateRanges.length - 1;
for (var i = 1; i < length; i++){
var previous = dateRanges[i-1];
var current = dateRanges[i];
// check for overlap, here
}
sorted.reduce((val, current, idx, arr) => {
if (idx === 0) { return; }
var previous = arr[idx-1];
});
var previousEnd = previous.end.getTime();
var currentStart = current.start.getTime();
var overlap = (previousEnd >= currentStart)
var result = sorted.reduce((result, current, idx, arr) => {
// get the previous range
if (idx === 0) { return result; }
var previous = arr[idx-1];
// check for any overlap
var previousEnd = previous.end.getTime();
var currentStart = current.start.getTime();
var overlap = (previousEnd >= currentStart);
// store the result
if (overlap) {
// yes, there is overlap
result.overlap = true;
// store the specific ranges that overlap
result.ranges.push({
previous: previous,
current: current
})
}
return result;
// seed the reduce
}, {overlap: false, ranges: []});
@TannerPerrien
Copy link

2.js could be simplified to a single line of code:

return previous.start.getTime() - current.start.getTime();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment