简体   繁体   English

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

[英]Javascript get weekdays between two specific weekdays

How can I get all weekday names between 2 weekdays as parameters? 如何获得2个工作日之间的所有工作日名称作为参数? It should also return accurately when it get past the 7 days. 超过7天后,它也应该准确返回。

My week format is: 我的星期格式是:

'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' “星期日”,“星期一”,“星期二”,“星期三”,“星期四”,“星期五”,“星期六”

Example and expected output below. 示例和预期的输出如下。 Thanks 谢谢

 function day(first, last) { 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('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" 

You could manipulate the array to avoid using loops. 您可以操纵数组以避免使用循环。 Code is commented for clarity. 为了清楚起见,对代码进行了注释。

 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')); 

I think I would just return two different cases depending on whether the range extended past the weekend. 我想我会根据范围是否超出周末而返回两种不同的情况。 This will just return the slice if start is earlier in the week. 如果开始是在一周的早些时候,它将只返回切片。 Otherwise it returns the two parts piecewise: 否则,它将分段返回两个部分:

 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')) 

Something like this: 像这样:

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;
}

Here is one way you might do it: 这是您可以执行的一种方法:

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"
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"

Try this way 试试这个

 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")) 

 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" 

I think you should set an index to every weekday. 我认为您应该为每个工作日设置一个索引。

function day(first, last) {
    var firstIndex;
    var lastIndex;
    var weekDays = [
        { index: 0, name: 'Monday' },
        { index: 1, name: 'Tuesday' },
        { index: 2, name: 'Wednesday' },
        { index: 3, name: 'Thursday' },
        { index: 4, name: 'Friday' },
        { index: 5, name: 'Saturday' },
        { index: 6, name: 'Sunday' }
    ];

    weekDays.forEach(function (item) {
        firstIndex = item.name.toLowerCase() === first.toLowerCase() ? item.index : firstIndex;
        lastIndex = item.name.toLowerCase() === last.toLowerCase() ? item.index : lastIndex;
    });

    if (firstIndex === undefined || lastIndex === undefined) { return; }

    var days = [];
    weekDays.forEach(function (item) {
        if (item.index >= firstIndex && item.index <= lastIndex) {
            days.push(item.name);
        }
    });

    console.log(days.join(', '));
}

Simple, but obvious and effective: 简单但明显有效:

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! 在线尝试!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM