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 >1 maja, 2022</span>
<span >16 czerwca, 2022</span>
<span >3 marca, 2022</span>
<span >3 lipca 2022</span>
<span >9 lipca 2021</span>
CodePudding user response:
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 >1 Maja, 2022</span>
<span >16 czerwca, 2022</span>
<span >3 marca, 2022</span>
<span >3 lipca 2022</span>
<span >9 lipca 2021</span>