如何在新日期函数中使用循环来返回星期几

问题描述 投票:0回答:2

我有这个代码,我想根据这个常量返回一周中的天数:

    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 iteration 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 天:

周一 -1.33 ℃ 阴云

周二 -2.34 ℃ 碎云

编辑:我做到了

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 iteration 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>`;
          });

但是我周二一整天都有时间。像这样:

周二//今天是周一 19.5 ℃ 晴空

星期二 // 好的 15.62 ℃ 晴空

周二//周三 16.01 ℃ 晴空

等等

javascript reactjs loops foreach
2个回答
1
投票

我认为这就是您正在寻找的 - 如何从时间戳中提取日期。

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])
})


1
投票
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" }));
© www.soinside.com 2019 - 2024. All rights reserved.