I have a for loop and I want to display the date for each of the results of this loop. I can do this now but it displays like this:
2018-04-19 12:00:00
And I want it to display like this:
19-04-2018
I am able to target substrings and get the layout I want that way:
forecastDayEl= sessionStorage.getItem("forecastDay");
var objDay = JSON.parse(forecastDayEl);
objDay.forEach(function(entry) {
var time = entry.substring(11, 13);
var days = entry.substring(8, 10);
var month = entry.substring(5, 7);
var year = entry.substring(0, 4);
});
But I am not sure how to add this to the existing for loop:
var forecastLength;
var forecastText;
var g;
forecastLength = objTemp.length;
forecastText = "<div class='forecastList'>";
for (g = 0; g < forecastLength; g++) {
forecastText += "<ul>" + "<li class='forecastDay'>" + objDay[g] + "</li>" + "<li class='forecastTemp'>" + objTemp[g] + "</li>" + "<li class='forecastIcon'>" + "<img id='icon'" + "src='/weather/icons/" + objIcon[g] + ".png'" + "alt='Weather icon'>" + "</li>" + "</ul>";
}
forecastText += "</div>";
//outputs result as list
document.getElementById("forecastList").innerHTML = forecastText;
forecastDayEl is outputting:
["2018-04-19 12:00:00","2018-04-20 12:00:00","2018-04-21 12:00:00","2018-04-22 12:00:00","2018-04-23 12:00:00"]
objDay is outputting:
(5) ["2018-04-19 12:00:00", "2018-04-20 12:00:00", "2018-04-21 12:00:00", "2018-04-22 12:00:00", "2018-04-23 12:00:00"]
0
:
"2018-04-19 12:00:00"
1
:
"2018-04-20 12:00:00"
2
:
"2018-04-21 12:00:00"
3
:
"2018-04-22 12:00:00"
4
:
"2018-04-23 12:00:00"
var days is outputting:
19
20
21
22
23
It would be ideal to avoid this hassle if I could do something like:
objDay[g].toDateString();
But this does not work as:
objDay[g].toDateString is not a function