sundsx
sundsx

Reputation: 618

Javascript Skip saturday and sunday looping in month

I need to create an array of objects connected to each day of the month excluding weekends. example: Monday -1, Tuesday-2, Wednesday-3, Thursday-4, Friday-5, Monday-8 and so on. // jump two days

I found this snippet very useful for my idea...

code:

function getDaysArray(year, month) {
  var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;

  numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  daysIndex = {
    'Sun': 0,
    'Mon': 1,
    'Tue': 2,
    'Wed': 3,
    'Thu': 4,
    'Fri': 5,
    'Sat': 6
  };
  index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
  daysArray = [];

  for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
    if (daysInWeek[index++] == "Sunday" || daysInWeek[index++] == "Saturday") {
      continue; //I tried: no success.
    }
    daysArray.push({
      "title": "Turn",
      "resourceid": "4",
      "start": year + "-" + month + "-" + (i + 1) + "+" + "08:00:00",
      "end": year + "-" + month + "-" + (i + 1) + "+" + "14:00:00",
      "internals": ground[i] // people from array to assign at specific date
    });

    if (index == 7) index = 0;
  }

  return daysArray;
}
console.log(getDaysArray(2019, 12));

Upvotes: 3

Views: 512

Answers (5)

Ivan86
Ivan86

Reputation: 5708

You can construct a date for the first and last day of the month and then loop through all the days while incrementing the date by one day on every iteration and checking whether the current day is Saturday or Sunday by calling .getDay().

Date.prototype.getDay()

Returns the day of the week (0-6) for the specified date according to local time.

0 and 6 represent Sunday and Saturday respectively.


Here is the code:

function getDaysArray(year, month) {
  let currDate = new Date(year, month-1, 1);
  let lastDate = new Date(year, month, 0);
  let lastDay = lastDate.getDate();
  let daysArray = [];
  
  for(let i=1; i < lastDay+1; i++) {
    if (currDate.getDay() != 0 && currDate.getDay() != 6) {  // Sunday and Saturday are 0 and 6 respectively
      daysArray.push({
        "title": "Turn",
        "resourceid": "4",
        "start": year + "-" + month + "-" + i + "+" + "08:00:00",
        "end": year + "-" + month + "-" + i + "+" + "14:00:00",
        "internals": "placeholder" // ground[i-1] // people from array to assign at specific date
      });
    }
    currDate.setDate(currDate.getDate() + 1);
  }
  return daysArray;
}

// example call for December 2019
console.log( getDaysArray(2019, 12) );

Upvotes: 1

Honey Sharma
Honey Sharma

Reputation: 91

You can build date using the Date() contructor and then use the getDay() method to check if it is satuday(6) or sunday(0).

function getDaysArray(year, month) {
var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;

  numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
  index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
  daysArray = [];

  for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
    var d=new Date(year+"-"+month+"-"+(i + 1))
     if(!(d.getDay()==0 || d.getDay()==6)){
      console.log(d)
       daysArray.push({
            "title":"Turn",
            "resourceid":"4",
            "start":year+"-"+month+"-"+(i + 1)+"+"+"08:00:00",
            "end":year+"-"+month+"-"+(i + 1)+"+"+"14:00:00",
            "internals": ground[i] // people from array to assign at specific date
        });
  }
    if (index == 7) index = 0;
 }
    return daysArray;
}
 console.log(getDaysArray(2019, 12));

Upvotes: 1

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

Try this:

  function getDaysArray(year, month) {
    var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;
    let cnt = -1;
    numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
    index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
    daysArray = [];

    for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
      if (daysInWeek[index] == "Sunday" || daysInWeek[index] == "Saturday") {
        index++
        cnt++
        if (index == 7) index = 0;
        continue
      }
      if (cnt == -1) {
        cnt = 0;
      }
      daysArray.push((cnt + 1) + '. ' + daysInWeek[index++]);
      cnt++;
      if (index == 7) index = 0;
    }

    return daysArray;
  }
  console.log(getDaysArray(2019, 12));

Upvotes: 1

Francois Verollet
Francois Verollet

Reputation: 140

Easier: just use your index variable. If it equals 0 or 6, then it's a weekend, so don't push the day.

function getDaysArray(year, month) {
    var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;

    numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
    index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
    daysArray = [];

    for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
    	if (index != 0 && index != 6) {
           daysArray.push({
                "title":"Turn",
                "resourceid":"4",
                "start":year+"-"+month+"-"+(i + 1)+"+"+"08:00:00",
                "end":year+"-"+month+"-"+(i + 1)+"+"+"14:00:00"
            });
      }
      
      index++;

      if (index == 7) index = 0;
    }

    return daysArray;
}
    
console.log(getDaysArray(2019, 12));

Upvotes: 1

asimshahiddIT
asimshahiddIT

Reputation: 330

you can use the following condition

 if(daysInWeek[(new Date(year+"-"+month+"-"+(i + 1))).getDay()]!='Sunday'&&daysInWeek[(new Date(year+"-"+month+"-"+(i + 1))).getDay()]!= 'Saturday') 

So, The Code would be following.

  function getDaysArray(year, month) {
   var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;

numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
daysArray = [];

for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
if(daysInWeek[(new Date(year+"-"+month+"-"+(i + 1))).getDay()]!=  'Sunday' &&       daysInWeek[(new Date(year+"-"+month+"-"+(i + 1))).getDay()]!=  'Saturday')   
daysArray.push({
            "title":"Turn",
            "resourceid":"4",

            "start":year+"-"+month+"-"+(i + 1)+"+"+"08:00:00",
            "end":year+"-"+month+"-"+(i + 1)+"+"+"14:00:00",
            "internals": 'Asim' // people from array to assign at specific date
        });

    if (index == 7) index = 0;
}

   return daysArray;
  }
   console.log(getDaysArray(2019, 12));

Upvotes: 1

Related Questions