Javascript获得两个特定工作日之间的工作日

时间:2018-09-20 02:07:00

标签: javascript date-range dayofweek days

如何获取2个工作日之间的所有工作日名称作为参数?超过7天后,它也应该准确返回。

我的星期格式为:

  

“星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,   “星期六”

下面的示例和预期输出。谢谢

/posts/2/comments/21

9 个答案:

答案 0 :(得分:2)

您可以操纵数组以避免使用循环。为了清楚起见,对代码进行了注释。

function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

  var firstIndex = week.indexOf(first);          //Find first day
  week = week.concat(week.splice(0,firstIndex)); //Shift array so that first day is index 0
  var lastIndex = week.indexOf(last);            //Find last day
  return week.slice(0,lastIndex+1);              //Cut from first day to last day
}

console.log(day('Tuesday', 'Thursday'));
console.log(day('Friday', 'Tuesday'));
console.log(day('Saturday', 'Monday'));

答案 1 :(得分:2)

我想我会返回两种不同的情况,具体取决于范围是否超出了周末。如果开始是在一周的早些时候,它将只返回切片。否则,它将分段返回两个部分:

function day(first, last) {
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    let start = week.indexOf(first)
    let end = week.indexOf(last)
    return (start > end)
        ? [...week.slice(start), ...week.slice(0, end+1)]
        : week.slice(start, end+1)
  
  }

  console.log(day('Tuesday', 'Thursday'))
  console.log(day('Friday', 'Tuesday'))
  console.log(day('Saturday', 'Monday')) 

答案 2 :(得分:1)

类似这样的东西:

function day(first,last) {
  var week=new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var i=week.indexOf(first), result=[];
  do {
    result.push(week[i]);
    i=(i+1) % week.length;
  } while (week[i]!==last);
  result.push(last);
  return result;
}

答案 3 :(得分:0)

这是您可以使用的一种方法:

function day(first, last) {
  var day = new Date();
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  //Double the array to account for going from end of the week to the beginning
  var weeks = week.concat(week);
  var dayArray = [];
  var activeDay = false;
  //Loop through the large week array. 
  for (var x=0; x<weeks.length; x++) {
     var day = weeks[x];
    //Start adding to the array on the first day
    if (day == first) {
        activeDay = true;
    }
    //Start adding to the array on the first day
    if (activeDay) {
        dayArray.push(day);
      //If the last day then exit
      if (day == last) {
        return dayArray;
      }
    }

  }
    //Return an empty array if no matches
    return [];
}

day('Tuesday', 'Thursday'); // output should be "Tuesday, Wednesday, Thursday"
day('Friday', 'Tuesday'); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
day('Saturday', 'Monday'); // output should be "Saturday, Sunday, Monday"

答案 4 :(得分:0)

function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

  for (i = 0; i < 7; i++) {
     if(week[i]==first){
        for(j=i ; i< 14 ;j++){ 
            console.log(week[j%7]);
            if(week[j]==end){
               return;
            }
        }

     }
  }

}

day('Tuesday', 'Thursday'); // output should be "Tuesday, Wednesday, Thursday"
day('Friday', 'Tuesday'); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
day('Saturday', 'Monday'); // output should be "Saturday, Sunday, Monday"

答案 5 :(得分:0)

尝试这种方式

function GetDays(first, last) {
  var day = new Date();
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var start_index=week.indexOf(first);
  var last_index=week.indexOf(last);
  if(start_index<last_index){
     return week.slice(start_index,last_index+1);
  }
  else{
     return [...week.slice(start_index),...week.slice(0,last_index+1)]
  }
}
console.log(GetDays("Sunday","Tuesday"))
console.log(GetDays("Sunday","Sunday"))

答案 6 :(得分:0)

SearchView

答案 7 :(得分:0)

我认为您应该为每个工作日设置一个索引。

function day(first, last) {
    var day = new Date();
    var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	var firstIdx = week.indexOf(first), lastIdx = week.indexOf(last);

	var result = [];
	if(lastIdx >= firstIdx) {
		for (var i = firstIdx; i <= lastIdx && i < week.length; i++)
			result.push(week[i]);
	}
	else {
		for (var i = firstIdx; i < week.length; i++)
			result.push(week[i]);
		for (var i = 0; i <= lastIdx && i < week.length; i++)
			result.push(week[i]);
	}
	return result;
}

alert(day('Tuesday', 'Thursday')); // output should be "Tuesday, Wednesday, Thursday"
alert(day('Friday', 'Tuesday')); // output should be "Friday, Saturday, Sunday, Monday, Tuesday
alert(day('Saturday', 'Monday')); // output should be "Saturday, Sunday, Monday"

答案 8 :(得分:0)

简单但明显有效:

function day(first, last) {
  var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  var found = false; 
  for(var i=0; i<week.length; i++) {
    if (!found) {
        if (week[i] == first) found = true;
    }
    if (found) {
        console.log(week[i]);
    }
    if (found && week[i] == last) {
        return;
    }
  }
}

Try it online!