We consider that I have the following dates:
from: 21/09/2019
to: 29/09/2019
The result I would like to have is such a thing:
[
{
"day": 21,
"month": 9,
"year": 2019
},
...
{
"day": 29,
"month": 9,
"year": 2019
}
]
If I had such a thing:
from: 21/09/2019
to: 7/10/2019
The result I would like to have is such a thing:
[
{
"day": 21,
"month": 9,
"year": 2019
},
...
{
"day": 30,
"month": 9,
"year": 2019
},
...
{
"day": 1,
"month": 10,
"year": 2019
}
{
"day": 7,
"month": 10,
"year": 2019
}
]
What I would like to do is print in an array, all the days between the start date and the end date.
I was trying something like this, but I'm having some problems:
let from = "23/09/2019";
let to = "7/10/2019";
function getDaysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
function calculate(from, to) {
let splitF = from.split("/");
let splitT = to.split("/");
let array = [];
let dF = parseInt(splitF[0]);
let dT = parseInt(splitT[0]);
let mF = parseInt(splitF[1]);
let mT = parseInt(splitT[1]);
let yF = parseInt(splitF[2]);
let yT = parseInt(splitT[2]);
let day, init, final;
while (true) {
if (mF === mT && yF === yT) {
day = dT - dF;
} else {
day = getDaysInMonth(mF, yF)-dF;
}
init = dF;
final = init + day;
Array.apply(null, { length: final + 1 })
.map(Number.call, Number)
.slice(init)
.reduce((prev, curr) => {
let obj = {
day: curr,
month: mF,
year: yF
};
array.push(obj);
return prev;
}, []);
if (mF === mT && yF === yT) break;
mF++;
dF = 1;
if (mF === 13) {
mF = 1;
yF++;
}
}
return array;
}
console.log(calculate(from, to));