I'm trying to create a function that converts the milliseconds to the date form which should be compatible to Turkish and formatted like "<2-digits of day>-<full name of month>-<4-digits of year>".
I've already tried the function .toLocaleDateString("tr-TR") but couldn't try the alternative libraries such as Momemtjs and Datejs that are server-included because I need this on the client-side. I've tried console.log() for every variable with or without .toString() and couldn't figure it out.
const dSelector = document.querySelectorAll(".date");
const formatDate = (de) => {
var monthNames = [
"Ocak", "Şubat", "Mart",
"Nisan", "Mayıs", "Haziran", "Temmuz",
"Ağustos", "Eylül", "Ekim",
"Kasım", "Aralık"
];
var date = new Date(de);
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return `${day}-${monthNames[monthIndex]}-${year}`;
}
dSelector.forEach((d) => {
d.innerHTML = formatDate(d.innerHTML);
});
The expected output: "`<day>-<month>-<year>"
The actual output: "NaN-undefined-NaN"