I'm running into an issue in javascript where certain dates are in a dd/mm/yyyy format and others are in yyyy-dd-mmT00000. I want them in dd/mm/yyyy but the problem is when I run through my code, I call the format method and it is specifically for dates like "2018-02-06T00:00:00". When I get a date like 02/06/2018, it runs through the format function and messes it up. Is there anyway I could make a conditional saying if (date is formatted) { dont format }, or could I change my format method in any way?
This is the format function:
formatDate: function (date) {
for (var i = 0; i < date.length; i++) {
var day = date.substring(8);
if (day.charAt(0) == 0) {
day = day.substring(1);
}
var month = date.substring(5, 7);
if (month.charAt(0) == 0) {
month = month.substring(1);
}
var year = date.substring(0, 4);
var finalDateString = month + "/" + day + "/" + year;
}
return finalDateString;
}
This is part of the create function I use to create a table to store the information:
if (todos[i].progress != 100) {
todos[i].completed_date = "";
var dateDisplay = todos[i].completed_date;
}
if (todos[i].progress == 100) {
var dateDisplay = todos[i].completed_date;
if (dateDisplay instanceof Date) {
dateDisplay = dateDisplay.toLocaleDateString();
} else {
dateDisplay = dateDisplay.slice(0, 10);
dateDisplay = formatDate(dateDisplay);
}
}