简体   繁体   English

如何从当前日期开始循环遍历天数数组

[英]How to loop through days array starting from the current date

I've built a simple app that prints days from an array.我构建了一个简单的应用程序,可以从数组中打印天数。 When you click the button, you print another day and another etc. However, when I add a starting point - the current day - I cannot loop through following days.当您单击该按钮时,您将打印另一天和另一天等等。但是,当我添加一个起点 - 当天 - 我无法循环播放接下来的几天。

 const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = 0; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[currentDay]; // this is the place where I'm stuck // I cannot write: days[currentDay]++ i++; if (i >= days.length) i = 0; list.append(newEl); });
 <h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>

You're using days[currentDay] .您正在使用days[currentDay] But since currentDay does not change, it won't work但由于currentDay没有改变,它不会工作


Lets go this way:让 go 这样:

  1. After getting currentDay , set i to currentDay (this is ok since your array has the correct order, we could use indexOf aswell.获得currentDay后,将i设置为currentDay (这没关系,因为您的数组具有正确的顺序,我们也可以使用indexOf

  2. Use i as index to show the date:使用i作为索引来显示日期:

 const days = [ "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday ", "Sunday ", ]; const btn = document.querySelector("#btn"); let d = new Date(); let currentDay = d.getDay(); const list = document.querySelector("#list"); let i = currentDay; btn.addEventListener("click", function () { let newEl = document.createElement("li"); newEl.textContent = days[i]; i++; if (i >= days.length) i = 0; list.append(newEl); });
 <h2>Days of the week:</h2> <ul id="list"></ul> <button id="btn">Add one more day</button>

The getDay() method returns the day of the week where 0 represents Sunday getDay()方法返回星期几,其中 0 代表星期日

Perhaps all you have to do is make Sunday the first element of the array也许您所要做的就是将星期日设为数组的第一个元素

Please try the following code请尝试以下代码

const days = [
    "Sun",
    "Mon",
    "Tue",
    "Wed",
    "Thu",
    "Fri",
    "Sat",
];

var currentDay = new Date().getDay();  // Sun = 0, Mon = 1, ..., Sat = 6 

console.log(days[currentDay]);

console.log(days[currentDay + 1]);

Hopefully I did not misunderstand your question希望我没有误解你的问题

Fabio法比奥

The issue was with the placement of ++ that you were trying.问题在于您尝试的++位置。 Also you don't require an extra variable i你也不需要额外的变量i

const days = [
  "Monday ",
  "Tuesday ",
  "Wednesday ",
  "Thursday ",
  "Friday ",
  "Saturday ",
  "Sunday ",
];

const btn = document.querySelector("#btn");

let d = new Date();
let currentDay = d.getDay();

const list = document.querySelector("#list");

btn.addEventListener("click", function () {
  let newEl = document.createElement("li");

  newEl.textContent = days[currentDay++];
 
  if (currentDay >= days.length) currentDay = 0;
  list.append(newEl);
});

You are incrementing i but you never change the currentDay .你正在增加i但你永远不会改变currentDay

In your function, remove i and use currentDay instead.在您的 function 中,删除i并改用currentDay

Here is the code:这是代码:

const days = [
    "Monday ",
    "Tuesday ",
    "Wednesday ",
    "Thursday ",
    "Friday ",
    "Saturday ",
    "Sunday ",
];

const btn = document.querySelector("#btn");

let d = new Date();
let currentDay = d.getDay();

const list = document.querySelector("#list");

btn.addEventListener("click", function () {

    currentDay++; // add 1 to currentDay
    if (currentDay > days.length) currentDay = 0; // reset currentDay to 0 each week

    let newEl = document.createElement("li");
    newEl.textContent = days[currentDay];  
    list.append(newEl);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算当前连续(从当前日期开始连续几天)? - How to calculate the current streak (consecutive days in a row starting at the current date)? 如何在Javascript中生成当前天数到最后几天的日期? - How to generate date from current days to last days in Javascript? JS中如何从索引开始遍历数组 - How to loop through an array starting at an index in JS 如何在javascript中从当前日期起7天创建一个星期几的数组(即0表示星期日,1表示星期一等)? - How to create an array of numbers of the days of the week (i.e. 0 for sunday, 1 for monday, etc.) 7 days from current date in javascript? 从当前月份开始的几个月中,您如何倒退? - How do you loop backwards through the months starting with the current one? 如何通过Java脚本获取日期天数(当前日期和将来日期)之间的差额 - How can we get the difference of date days (current date and future date) through Java script 如何从当前日期到最近5天从数据库获取值 - How to get values from database from current date to last 5 days 如何从现在起更改当前日期-7天-[PersianDatepicker] - How to change current date -7 days from now - [PersianDatepicker] Moment JS - 如何从当前日期减去 7 天? - Moment JS - how to subtract 7 days from current date? 如何过滤从当前日期到 90 天前的 createdAt? - How to filter createdAt from current date to 90 days ago?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM