I am trying to fetch all the months and year from a specific date to current date last month, for example : fetching all the months from January, 2019 to December 2022 i.e., current year and my function looks something like this :
const getDates = () => {
let initialDate = new Date("01/01/2019");
const currentDate = new Date(`12/01/${new Date().getFullYear()}`);
let i = 0;
let dates = [];
while (initialDate <= currentDate) {
initialDate.setMonth(i++);
dates.push(initialDate.toDateString());
}
return dates;
};
console.log(getDates());
Output :
Array [
"Tue Jan 01 2019",
"Fri Feb 01 2019",
"Fri Mar 01 2019",
"Mon Apr 01 2019",
"Wed May 01 2019",
"Sat Jun 01 2019",
"Mon Jul 01 2019",
"Thu Aug 01 2019",
"Sun Sep 01 2019",
"Tue Oct 01 2019",
"Fri Nov 01 2019",
"Sun Dec 01 2019",
"Wed Jan 01 2020",
"Mon Feb 01 2021",
"Tue Mar 01 2022",
"Sat Apr 01 2023",
]
But it gives unusual results after the 15th iteration and I don't know why.