简体   繁体   English

Javascript跳过月中的周六和周日循环

[英]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.例如:星期一-1、星期二-2、星期三-3、星期四-4、星期五-5、星期一-8 等等。 // 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));

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

Easier: just use your index variable.更简单:只需使用您的index变量。 If it equals 0 or 6, then it's a weekend, so don't push the day.如果它等于 0 或 6,那么它是周末,所以不要推一天。

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

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

You can build date using the Date() contructor and then use the getDay() method to check if it is satuday(6) or sunday(0).您可以使用 Date() 构造函数构建日期,然后使用 getDay() 方法检查它是 satuday(6) 还是 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));

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() .您可以为该月的第一天和最后一天构造一个日期,然后循环遍历所有日期,同时在每次迭代时将日期增加一天,并通过调用.getDay()检查当前日期是星期六还是星期日。

Date.prototype.getDay()日期.prototype.getDay()

Returns the day of the week (0-6) for the specified date according to local time.根据当地时间返回指定日期的星期几 (0-6)。

0 and 6 represent Sunday and Saturday respectively. 0 和 6 分别代表星期日和星期六。


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

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

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