Javascript 获取两个特定工作日之间的工作日
Javascript get weekdays between two specific weekdays
如何获取 2 个工作日之间的所有工作日名称作为参数?当它超过 7 天时,它也应该 return 准确。
我的周格式是:
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday'
示例和预期输出如下。谢谢
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"
像这样:
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;
}
您可以操纵数组以避免使用循环。为清楚起见,对代码进行了注释。
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'));
我想我会 return 两种不同的情况,具体取决于周末范围是否延长。如果本周早些时候开始,这将只是 return 切片。否则它 return 将两部分分段:
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'))
您可以采用以下一种方法:
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"
试试这个方法
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"
我认为你应该为每个工作日设置一个索引。
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(', '));
}
简单、明显且有效:
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;
}
}
}
如何获取 2 个工作日之间的所有工作日名称作为参数?当它超过 7 天时,它也应该 return 准确。
我的周格式是:
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
示例和预期输出如下。谢谢
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"
像这样:
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;
}
您可以操纵数组以避免使用循环。为清楚起见,对代码进行了注释。
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'));
我想我会 return 两种不同的情况,具体取决于周末范围是否延长。如果本周早些时候开始,这将只是 return 切片。否则它 return 将两部分分段:
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'))
您可以采用以下一种方法:
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"
试试这个方法
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"
我认为你应该为每个工作日设置一个索引。
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(', '));
}
简单、明显且有效:
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;
}
}
}