Reputation: 1044
If I have an array of days:
var days=[2,5] (corresponds to Tuesday and Friday)
How can I find out how many days in the array fall in a clientevent such as the following one:
var holiday= {title: holiday,
start: 2015-10-20,
end: 2015-10-30}
I don't need to know how many of each day falls in the event, but only the total. So in this case the script would return '4' (2 tuesdays and 2 fridays in the event).
Your help is much appreciated!
Upvotes: 1
Views: 230
Reputation: 11725
You can loop through the dates and see if the arr.indexOf
the current day of week is >= 0
, look:
var arr = [2, 5];
var holiday = {
title: holiday,
start: '2015-10-20',
end: '2015-10-30'
}
var st = new Date(holiday.start),
en = new Date(holiday.end);
var cont = 0;
while (st <= en) {
if (arr.indexOf(st.getUTCDay()) >= 0) {
cont++;
}
st.setDate(st.getDate() + 1);
}
console.log(cont);
Note: you must use getUTCDay()
instead of getDay()
because of the Time Zone differences.
Upvotes: 1