I am trying to sum up each member's total absent days.
Let's have only two members: A and B. A had left two times for different reasons, she went holiday off for 3 days and some other reason for 2 days. B had been hospitalized for 8 days, went vacation for 4 days.
So, here is the code I did my best.
*The dates I wrote is for example. Date values will be posted by JS onclick. No need of toString or something else.
var member = [{
name : "A",
absent : [{
reason : "Leave",
start_date : "2020-02-01",
end_date : "2020-02-03"
},{
reason : "etc",
start_date : "2020-02-15",
end_date : "2020-02-16"
}
]
},{
name : "B",
absent : [{
reason : "Hospitalized",
start_date : "2020-02-03",
end_date : "2020-02-10"
},{
reason : "Leave",
start_date : "2020-02-10",
end_date : "2020-02-13"
}
]
}]
var countAbsentDays = function(){
var countDays_start = member[0].absent[0].start_date;
var countDays_end = member[0].absent[0].end_date;
var diff = (new Date(countDays_end)) - (new Date(countDays_start));
var count = diff/(1000 * 60 * 60 * 24);
return count;
};
console.log(countabsentDays());
To collect each member's total absent days, I need to develop the code below using for-loop.
var countDays_start = member[0].absent[0].start_date;
Guess, the index number I put 0 could be replaced with variable. But I can't picture how this function runs all over absent array and its element absent.start_date and absent.end_date.
Anybody help?