最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Looping Through Days of Week in JavaScript - Stack Overflow

programmeradmin4浏览0评论

So I have tried this example from W3Schools and I get it.

function day() {
  var day = new Date();
  var week = new Array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  );

  for (i = 0; i < week.length; i++) {
    console.log(week[day.getDay(i) + 1]);
  }

}

day();

So I have tried this example from W3Schools and I get it.

function day() {
  var day = new Date();
  var week = new Array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  );

  for (i = 0; i < week.length; i++) {
    console.log(week[day.getDay(i) + 1]);
  }

}

day();

The "+1" gets you tomorrows date, but what happens when you reach the end of the loop? Say today is Friday and I want to get three days out and Monday es back as "undefined" because it is past the loop.

Is there any way to start the loop over? From what I've seen, continue/break doesn't work.

Any better suggestions on how to get today's date and the next few so that a loop doesn't break?

Share Improve this question edited Dec 22, 2017 at 5:46 Rajesh 25k5 gold badges50 silver badges83 bronze badges asked Dec 22, 2017 at 5:41 Ben ThompsonBen Thompson 371 silver badge6 bronze badges 4
  • 1 getDay as its name suggest is a getter function and does not takes any arguments. So you are always getting same day irrespective of i – Rajesh Commented Dec 22, 2017 at 5:44
  • So you want to skip n days or just weekends? – dork Commented Dec 22, 2017 at 5:46
  • 2 Possible duplicate of How to access array in circular manner in javascript – 31piy Commented Dec 22, 2017 at 5:46
  • Use reminder: return week[(day.getDay(i) + 1) % 7] – Nir Alfasi Commented Dec 22, 2017 at 5:47
Add a ment  | 

3 Answers 3

Reset to default 3

i will not change the day because .getDay() does not process any arguments. Use the modulus operator % to wrap around the 7 days of the week. Run the snippet below to see that it works.

function day() {
  var day = new Date();
  var week = new Array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  );

  for (i = 0; i < 14; i++) {
    console.log(week[(day.getDay() + 1 + i) % 7]);
  }
}

day();

I found this to be the best way to create each day of the week in a loop:

function days() {
  var day = new Date();
  var week = new Array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  );

  for (i = 0; i < 7; i++) {
    console.log(week[(day.getDay() + i) % 7]);
  }
}

days();

Use '%' operator.

function day() {
var day = new Date();
var week = new Array(
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
);

for (i=0; i<week.length; i++) {
     var num = (day.getDay(i)+3)%7;
    console.log(week[num]);
}

}
发布评论

评论列表(0)

  1. 暂无评论