If I understand what you are trying to achieve, I think that this might do it.
I understand your problem as such:
Pass a date into a function. Iterate over an array of start times and end times and evaluate if the passed date lies between the start time and end time.
I am just learning JavaScript myself, so I am relying on CoffeeScript.
Here is the CoffeeScript version which I find easier to read than JavaScript:
# Usage: zip(arr1, arr2, arr3, ...)
zip = () ->
# zips iterables a la Python's zip
# https://coffeescript-cookbook.github.io/chapters/arrays/zip-function
lengthArray = (arr.length for arr in arguments)
length = Math.min(lengthArray...)
for i in [0...length]
arr[i] for arr in arguments
MINUTES = 60000 # factor to convert to milliseconds
now = Date.now() # milliseconds since epoch time
durations = new Array(90, 120, 180, 240, 190) # minutes
start_times = new Array()
start_times.push(now)
# convert to milliseconds
start_times.push(now + (duration * MINUTES)) for duration in durations
time_periods = new Array(30, 150, 90, 45, 25, 60) # minutes
end_times = new Array()
for time_periods__start_times in zip(time_periods, start_times)
time_period = time_periods__start_times[0]
start_time = time_periods__start_times[1]
end_times.push(start_time + (time_period * MINUTES))
is_between_periods = (date)->
for end_time__start_time in zip(end_times, start_times)
end_time = end_time__start_time[0]
start_time = end_time__start_time[1]
overlaps = date >= start_time and date <= end_time
date = new Date(date)
start = new Date(start_time)
end = new Date(end_time)
console.log("date: #{date}\nstart: #{start}\nend: #{end}\noverlaps: #{overlaps}")
is_between_periods(now)
The result I get from passing now
into the is_between_periods
function is as follows:
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 16:43:38 GMT+0000 (UTC)
overlaps: true
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 17:43:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 20:13:38 GMT+0000 (UTC)
overlaps: false
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 18:13:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 19:43:38 GMT+0000 (UTC)
overlaps: false
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 19:13:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 19:58:38 GMT+0000 (UTC)
overlaps: false
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 20:13:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 20:38:38 GMT+0000 (UTC)
overlaps: false
date: Tue Feb 23 2016 16:13:38 GMT+0000 (UTC)
start: Tue Feb 23 2016 19:23:38 GMT+0000 (UTC)
end: Tue Feb 23 2016 20:23:38 GMT+0000 (UTC)
overlaps: false
Here is the JavaScript produced from the CoffeeScript:
// Generated by CoffeeScript 1.10.0
var MINUTES, duration, durations, end_times, is_between_periods, j, k, len, len1, now, ref, start_time, start_times, time_period, time_periods, time_periods__start_times, zip;
zip = function() {
var arr, i, j, length, lengthArray, ref, results;
lengthArray = (function() {
var j, len, results;
results = [];
for (j = 0, len = arguments.length; j < len; j++) {
arr = arguments[j];
results.push(arr.length);
}
return results;
}).apply(this, arguments);
length = Math.min.apply(Math, lengthArray);
results = [];
for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
results.push((function() {
var k, len, results1;
results1 = [];
for (k = 0, len = arguments.length; k < len; k++) {
arr = arguments[k];
results1.push(arr[i]);
}
return results1;
}).apply(this, arguments));
}
return results;
};
MINUTES = 60000;
now = Date.now();
durations = new Array(90, 120, 180, 240, 190);
start_times = new Array();
start_times.push(now);
for (j = 0, len = durations.length; j < len; j++) {
duration = durations[j];
start_times.push(now + (duration * MINUTES));
}
time_periods = new Array(30, 150, 90, 45, 25, 60);
end_times = new Array();
ref = zip(time_periods, start_times);
for (k = 0, len1 = ref.length; k < len1; k++) {
time_periods__start_times = ref[k];
time_period = time_periods__start_times[0];
start_time = time_periods__start_times[1];
end_times.push(start_time + (time_period * MINUTES));
}
is_between_periods = function(date) {
var end, end_time, end_time__start_time, l, len2, overlaps, ref1, results, start;
ref1 = zip(end_times, start_times);
results = [];
for (l = 0, len2 = ref1.length; l < len2; l++) {
end_time__start_time = ref1[l];
end_time = end_time__start_time[0];
start_time = end_time__start_time[1];
overlaps = date >= start_time && date <= end_time;
date = new Date(date);
start = new Date(start_time);
end = new Date(end_time);
results.push(console.log("date: " + date + "\nstart: " + start + "\nend: " + end + "\noverlaps: " + overlaps));
}
return results;
};
is_between_periods(now);