我有一个for循环,我想显示此循环的每个结果的日期。我现在可以这样做,但它显示如下:
2018-04-19 12:00:00
我希望它显示如下:
19-04-2018
我能够定位子串并获得我想要的布局:
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);
});
但我不知道如何将其添加到现有的for循环中:
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正在输出:
["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正在输出:
(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输出:
19
20
21
22
23
如果我可以做以下事情,那么避免这种麻烦是理想的:
objDay[g].toDateString();
但是这不起作用:
objDay [g] .toDateString不是函数
答案 0 :(得分:1)
使用The consul service terminated with the following service-specific error:
Incorrect function.
将.map
转换为所需格式的字符串数组,然后使用转换后的数组:
objDay
&#13;
答案 1 :(得分:0)
将2018-04-19 12:00:00转换为19-04-2018的最简单方法是获取值并重新排序。有很多方法,例如。
sudo -H pip3 install pip==8.1.1
&#13;
等等......
答案 2 :(得分:-1)
objDay.forEach(function(e, i) {
objDay[i] = (new Date(e)).toLocaleDateString('es-DO').replace(/\//g, '-');
});