Javascript get weekdays between two specific weekdays

Viewed 2477

How can I get all weekday names between 2 weekdays as parameters? It should also return accurately when it get past the 7 days.

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"

9 Answers

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

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

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

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!

Related