如何在 return 星期几的新日期函数中使用循环

How to use loop in new date function to return day of week

我有这段代码,我想 return 基于这个常量的星期几:

    const dayOfWeek = {
              '0': 'Sun',
              '1': 'Mon',
              '2': 'Tue',
              '3': 'Wed',
              '4': 'Thu',
              '5': 'Fri',
              '6': 'Sat',
            }

 info.forEach(function (information, counter) {
            // Get date
            var dateConvert = new Date()
            var date = (dateConvert.getDay());
            // Check if first itteration or if the row is full
            if (counter === 0 || row.querySelectorAll("p").length === infoPerRow) {

              // Create new row and class
              row = document.createElement("div");
              row.classList.add("row");
              rowCity = document.createElement("h1");
              rowCity.classList.add("rowCity");

              //Append the row to the fragment
              fragment.appendChild(rowCity);
              fragment.appendChild(row);

            }
            console.log(row);
            //Update the content of row
            rowCity.innerHTML = `<h2>${name} - ${country}</h2><br><form onSubmit={this.handleDelete}><button type="submit" id="add" onClick={this.handleDelete}>Remove City</button></form><br>`;
            row.innerHTML += `<div><h3>${dayOfWeek[date]}</h3><br><h2>${iconCodes[information.icon]}</h2><br><h5>${information.temp} ºC</h5><br><h6>${information.description}</h6></div>`;
          });

我希望在这个循环中有这样的东西。那是8天。第一个是当天,然后是接下来的 7 天:

Mon -1.33 ºC overcast clouds

Tue -2.34 ºC broken clouds

编辑:我做到了

info.forEach(function (information, counter) {
            // Get date
            var dateConvert = new Date(information.dt)
            var date = (dateConvert.toLocaleString("en-us", { weekday: "short" }));
            console.log(date)
            // Check if first itteration or if the row is full
            if (counter === 0 || row.querySelectorAll("p").length === infoPerRow) {

              // Create new row and class
              row = document.createElement("div");
              row.classList.add("row");
              rowCity = document.createElement("h1");
              rowCity.classList.add("rowCity");

              //Append the row to the fragment
              fragment.appendChild(rowCity);
              fragment.appendChild(row);

            }
            console.log(row);
            //Update the content of row
            rowCity.innerHTML = `<h2>${name} - ${country}</h2><form onSubmit={this.handleDelete}><button class="btn" type="submit" id="add" onClick={this.handleDelete}>Remove City</button></form><br>`;
            row.innerHTML += `<div><h3>${date}</h3><br><h2>${iconCodes[information.icon]}</h2><br><h5>${information.temp} ºC</h5><br><h6>${information.description}</h6></div>`;
          });

但我星期二整天都在。像这样:

Tue // Here is today, Mon 19.5 ºC clear sky

Tue // Ok here 15.62 ºC clear sky

Tue // Wed 16.01 ºC clear sky

等等

const dayOfWeek = {
  "0": "Sun",
  "1": "Mon",
  "2": "Tue",
  "3": "Wed",
  "4": "Thu",
  "5": "Fri",
  "6": "Sat",
};
const nextDay = input => {
  const output = new Date(input.valueOf());
  output.setDate(output.getDate() + 1);
  return output;
};

let day = new Date();
for (let i = 0; i < 7; i++) {
  console.log(day.toLocaleString("en-us", { weekday: "short" }));
  console.log(dayOfWeek[day.getDay()]);

  day = nextDay(day);
}

2条日志语句输出相同。 第一个在没有对象的情况下产生你想要的结果,第二个通过索引你的常量

编辑: 或者在你有的循环中做...

let today = new Date();
const addDays = (input, days) => {
      const output = new Date(input.valueOf());
      output.setDate(output.getDate() + days);
      return output;
    };
 info.forEach(function (information, counter) {
            var day = addDays(today, counter);

            // render details
          });

最后...直接来自信息数据

const info = { dt: 1644245080 };
const date = new Date(info.dt);
console.log(date.toLocaleString("en-us", { weekday: "short" }));

我想这就是您要找的 - 如何从时间戳中提取日期。

const dayOfWeek = {
  0: "Sun",
  1: "Mon",
  2: "Tue",
  3: "Wed",
  4: "Thu",
  5: "Fri",
  6: "Sat"
};

const info = [
  {dt: 1644245080, temp: 0.06, description: "scattered clouds", icon: "03d"},
  {dt: 1644339600, temp: -2.34, description: "broken clouds", icon: "04d"},
  {dt: 1644426000, temp: 1.7, description: 'overcast clouds', icon: '04d'},
  {dt: 1644512400, temp: -0.06, description: 'broken clouds', icon: '04d'},
  {dt: 1644598800, temp: -2.18, description: 'light snow', icon: '13d'}
];


info.forEach(function (information, counter) {
  var dayNum = new Date(information.dt * 1000).getDay();
  console.log(dayOfWeek[dayNum])
})