Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Javascript Date Object giving out some unusual results

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!