I have a list of dates that I need to convert into a date range, slicing out the Month values that aren't on the first date of the date ranges, while also adding the hours accumulated for the days within the day portion.
My example array is:
["Nov 23 2021 8 hrs",
"Nov 24 2021 8 hrs",
"Nov 27 2021 8 hrs",
"Dec 3 2021 8 hrs"]
I am trying to get the end array to look like this:
["Nov 23-24 2021 16hrs, Nov 27 2021 8hrs, Dec 3 2021 8hrs"]
So far I was able to get the dates to concatenate correctly with this code just passing in an array that contains only numbers
const getDayRanges = (dayArr) => {
var ranges = [],
rstart,
rend;
for (var i = 0; i < dayArr.length; i++) {
console.log("days" ,dayArr[i])
rstart = dayArr[i];
rend = rstart;
while (dayArr[i + 1] - dayArr[i] == 1) {
rend = dayArr[i + 1]; // increment the index if the numbers sequential
i++;
}
ranges.push(rstart == rend ? rstart + "" : rstart + " to " + rend);
}
return ranges;
};
But I have not been about to figure out how to slice out the Months correctly to place them in a new array with the hyphenated date numbers, let alone how to then add up all of the hours portion that align with the dates that are concatenated.
Any help would be extremely appreciated.