从今天和一周以后产生工作日

时间:2018-08-15 19:34:04

标签: javascript jquery

我要为一周中的每一天生成div:

var days = new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); 

function generateWeekdays() {
  var d = new Date();
  var weekday = d.getDay();
  var todaysWeekday = days[weekday];

  for (var i = weekday; i < days.length; i++) {
      $('<div>' + days[i] + '</div>').appendTo('#weekdayList');

  }
}


 generateWeekdays();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="weekdayList"></div>

(例如)今天(星期三)和星期六(最后一个索引)生成该索引,但是如何使它生成一个星期,所以它一直生成到下周的星期三,然后在此之后停止?

7 个答案:

答案 0 :(得分:3)

您可以使用模块运算符%来使索引保持界限。这将使超出数组长度的所有内容返回,因此您可以从0到数组长度加上当前日期:

var days = new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); 

function generateWeekdays() {
  var d = new Date();
  var weekday = d.getDay();

  for (var i = 0; i < days.length; i++) {
     console.log(days[(i + weekday) % days.length])

  }
}
generateWeekdays()

问题尚不完全清楚,但是如果您想在周三加入 next ,则可以使用for (var i = 0; i < days.length + 1; i++)

进行循环

答案 1 :(得分:1)

您可以迭代7次(根据需要一周),而不必在days.length处停止,并进行模(%7)运算以从days数组中获取元素。

var days = new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); 

function generateWeekdays() {
  var d = new Date();
  var weekday = d.getDay();
  var todaysWeekday = days[weekday];

  for (var d = weekday, i = 0; i < 7; d++, i++) {
      $('<div>' + days[d % 7] + '</div>').appendTo('#weekdayList');

  }
}

generateWeekdays();

答案 2 :(得分:1)

如何削减日期(从正常工作日开始)并在一周的末尾(那些天)追加(从今天开始,您必须从该日期开始削减)

function getWeekDaysFromToday() {
  let days = ["SUN","MON","TUE","WED","THU","FRI","SAT"];
  return days.concat( days.splice (0,new Date().getDay()));
}
console.log(getWeekDaysFromToday())

答案 3 :(得分:0)

在您的for循环中,而不是进行迭代直到days.length,您只想进行7次迭代并循环回到数组的开头。如果您设置了一个将迭代7次的计数器,并且在循环中添加了if语句,如果到达末尾则循环回到数组的开头,那应该可行。

答案 4 :(得分:0)

考虑到今天与数组索引有关的内容,如何添加另一个迭代?

var days = new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); 

function generateWeekdays() {
  var d = new Date();
  var weekday = d.getDay();
  var todaysWeekday = days[weekday];

  for (var i = weekday; i < days.length; i++) {
      $('<div>' + days[i] + '</div>').appendTo('#weekdayList');

  }
  
  for(var i = 0; i < days.indexOf(days[weekday]); i++){
     $('<div>' + days[i] + '</div>').appendTo('#weekdayList');
  }
}


 generateWeekdays();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="weekdayList"></div>

答案 5 :(得分:0)

这是我一起提出的快速实施方案。我使用了一个do / while循环,因为您知道您总是至少要执行一次。我还将循环限制为要显示的天数,因此您可以增加显示天数。我还使用了%(模量)与count和weekday来从数组中获取正确的值。

var days = new Array("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");

function generateWeekdays() {
  var d = new Date();
  var weekday = d.getDay();
  var todaysWeekday = days[weekday];

  let count = 0;
  do {
    $('<div>' + days[(count + weekday) % days.length] + '</div>').appendTo('#weekdayList');

    count++;
  } while (count < 7)

}

generateWeekdays()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="weekdayList">

</div>

答案 6 :(得分:0)

我对这个问题采取了不同的方法。我没有逐步检查是否已经处理了7天,而是将天数列表连接到自身,所以它将是14天,然后从当天开始,我将7天拼接起来,所以我有一周的时间,对于每个我创建了一个div。

我还对其进行了更改,以收集div并将它们全部附加到末尾,以提高性能。

var days = new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); 

function generateWeekdays() {
  var oneWeekOfDays = days.concat(days).splice(new Date().getDay(), 7).map(function(day){
    return '<div>'+ day +'</div>';
  });
  
  $('#weekdayList').append(oneWeekOfDays);
}

generateWeekdays();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="weekdayList"></div>