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

97
Views
Invalid Date in foreach

I don't understand why one date is good and the rest show "Invalid Date".

const blocksDate = document.querySelectorAll(".roadmap__time");
blocksDate.forEach((block) => {
  const dateString = block.innerHTML;
  const dateBlock = new Date(dateString);
  const currentDate = new Date();
  console.log(dateBlock);
});
<span class="roadmap__time">1 maja, 2022</span>
<span class="roadmap__time">16 czerwca, 2022</span>
<span class="roadmap__time">3 marca, 2022</span>
<span class="roadmap__time">3 lipca 2022</span>
<span class="roadmap__time">9 lipca 2021</span>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The reason 1 mar(ca) works is that the Date.parse sees only the first 3 chars and guesses March

Here is a script that converts both ways

const months = ["STY", "LUT", "MAR", "KWIE", "MAJ", "CZE", "LIP", "SIE", "WRZ", "PAZ", "LIS", "GRU"]
const options = {
  year: "numeric",
  month: "long",
  day: "numeric"
};

const blocksDate = document.querySelectorAll(".roadmap__time");
blocksDate.forEach((block) => {
  const dateString = block.textContent;
  const [_, dd, mmm, yyyy] = dateString.match(/(\d{1,2}) (\w{3})\w+,? (\d{4})/)
  console.log(dd, mmm, yyyy)
  const dateBlock = new Date(yyyy, months.indexOf(mmm.toUpperCase()), dd, 15, 0, 0, 0);
  console.log(dateBlock);
  const plDate = dateBlock.toLocaleDateString("pl", options)
  console.log(plDate)

});
<span class="roadmap__time">1 Maja, 2022</span>
<span class="roadmap__time">16 czerwca, 2022</span>
<span class="roadmap__time">3 marca, 2022</span>
<span class="roadmap__time">3 lipca 2022</span>
<span class="roadmap__time">9 lipca 2021</span>

about 4 years ago · Juan Pablo Isaza Report
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!