noneJavaScript
noneJavaScript

Reputation: 845

How to loop through weekdays in a month?

monthDays = 31;
dayOfMonth = 9;
weekOfMonth = 2;

startDay = weekStartingDate (weekOfMonth); // function return 8

// startDay could be 8, 15, 22 or 28
for (var day = startDay; day < (startDay+7) ; day++)
{
     //stuff
}

The problem is, when startDay is 29+, that (startDay+7) exceds monthDays

I want to loop through days considering weekdays ranges.

Upvotes: 1

Views: 1111

Answers (5)

Owen
Owen

Reputation: 718

Why not use a tertiary?

monthDays = 31;
dayOfMonth = 9;
weekOfMonth = 2;

startDay = weekStartingDate (weekOfMonth); // function return 8
var maxDay = (startDay+7) > monthDays ? monthDays : (startDay+7)
// startDay could be 8, 15, 22 or 28
for (var day = startDay; day <= maxDay ; day++)
{
     //stuff
}

Upvotes: 1

RobG
RobG

Reputation: 147403

Not quite sure what you are trying to do, but it seem you are trying to get the dates for a week that are in the same month.

The functions below do that. getWeekStartDate returns a Date for the start of the week for a given date, optionally starting on Monday or Sunday. getNext7DatesInMonth gets up to 7 days from the given a date in the same month.

The result is an array of numbers for the required dates.

/*
** @param {Date} date
** @param {boolean} weekStartsOnMon - true if week starts on Monday
** @returns {Date} - new date object for first day of week
*/
function getWeekStartDate(date, weekStartsOnMon) {
  var d = new Date(+date);
  var dayNum = d.getDay();

  // If start of week is Monday
  if (weekStartsOnMon) {
    d.setDate(d.getDate() - (dayNum? dayNum : 7) +1)
    ;
  // If start of week is Sunday
  } else {
    d.setDate(d.getDate() - d.getDay());
  }
  return d;
}

/*
** For the given date, get the dates in the week for the same month.
**
** @param {Date} date
** @param {boolean} weekStartsOnMon - true if week starts on Monday
** @returns {Array} - String dates for rest of week in same month as date
*/
function getNext7DatesInMonth(date){
  var start = new Date(+date);
  var monthNum = start.getMonth();
  var weekDates = [];
  var i = 7;

  while (monthNum == start.getMonth() && i--) {
    weekDates.push(start.getDate());
    start.setDate(start.getDate() + 1);
  }
  return weekDates;
}

// Start week on Sunday
var d = new Date(2015,4,31)
console.log(d + ': ' + getNext7DatesInMonth(getWeekStartDate(d, false))); // 31

// Start week on Monday
var d = new Date(2015,4,31)
console.log(d + ': ' + getNext7DatesInMonth(getWeekStartDate(d, true))); // 25,26,27,28,29,30,31

You could do a similar function without Date objects based on getting the number of days in the month, but Date objects are convenient.

Upvotes: 0

Ritesh  Karwa
Ritesh Karwa

Reputation: 2254

monthDays = 31;
dayOfMonth = 9;
weekOfMonth = 2;

startDay = weekStartingDate (weekOfMonth); // function return 8
for (var day = startDay; day < (startDay+7) ; day++)
{
     //stuff
  if(x >=31 ){
  break; 
         }
}

Upvotes: 1

user2755140
user2755140

Reputation: 2027

You should define the limit value to your for loop

for (var day = startDay; day < ((startDay+7) > monthDays ? monthDays : (startDay+7)) ; day++)
{
     //stuff
}

Upvotes: 2

omikes
omikes

Reputation: 8513

Instead of (startDay+7) use (startDay+7)%monthDays

Upvotes: 0

Related Questions