繁体   English   中英

如何使用 Pure JavaScript 以 14 天的重复模式在开始日期和结束日期之间获取选定的工作日日期

[英]how to get selected week days dates between Start date and end date with 14 day recurrence pattern using Pure JavaScript

我需要返回一些日期 object,它发生在开始日期和结束日期之间,具有重复模式,并使用纯 javaScript(无 postman 或时刻)给出工作日。 我能够获得 1 周的日期 object。 但我无法获得日期 object 超过一周(14 或 21 天)。 这是我的以下代码。

 function dates{
            var startDate = new Date("2020/06/22"); 
            var endDate = new Date("2020/08/16");
            var recurrencePattern = 14 ;// 2 weeks
            var daysSelected = [0,1]; //0 - sunday , 1 - monday
            var datesObj = [];
            while(startDate <= endDate){
            var i=0;
            while(i<daysSelected.length){
                if(startDate.getDay() ==  daysSelected[i]){
                   datesObj.push(startDate.getFullYear()+"-"+(startDate.getMonth()+01)+"-"+startDate.getDate());
                }
                i++;
            }
                startDate.setDate(startDate.getDate() +1);
            }
            return datesObj;
          }

我应该得到的 output(每 2 周,在 2020 年 6 月 22 日和 2020 年 8 月 16 日之间选择的周日和周一)是

datesObj["22-06-2020","5-07-2020","6-07-2020","19-07-2020","20-07-2020","2-08-2020","3-08-2020","16-08-2020"]

但我让所有星期日和星期一的日期都从开始日期和结束日期开始。 任何人都可以解决这个问题。

您可以使用以下代码更新您的while(i<daysSelected.length){... }代码。

if (daysSelected.includes(startDate.getDay())) {
  datesObj.push(startDate.getFullYear() + "-" + (startDate.getMonth() + 01) + "-" + startDate.getDate());
}

此外,当您遇到last day of weekstartDate.getDay() == 6时,要跳过recurrencePattern = 14天,然后跳过recurrencePattern-7+1天。 您需要从recurrencePattern中减去7 ,因为您已经使用loop处理了one week 还使用datesObj.length > 0因此它只会跳过actually为相应weeks添加dates的周。

下面是完整的代码。 你可以检查一下。

 function dates(startDate, endDate) { var recurrencePattern = 14; // 2 weeks var daysSelected = [0, 1]; //0 - sunday, 1 - monday var datesObj = []; while (startDate <= endDate) { if (daysSelected.includes(startDate.getDay())) { datesObj.push(startDate.getFullYear() + "-" + (startDate.getMonth() + 01) + "-" + startDate.getDate()); } if (datesObj.length > 0 && startDate.getDay() == 6) { startDate.setDate(startDate.getDate() + (recurrencePattern - 7 + 1)); } else { startDate.setDate(startDate.getDate() + 1); } } return datesObj; } console.log(dates(new Date("2020/06/22"), new Date("2020/08/16")));

更新var daysSelected = [0, 4]; 包括04之间的所有天数。

 function dates(startDate, endDate) { var recurrencePattern = 14; // 2 weeks var daysSelected = [0, 4]; //0 - sunday to 4 - thursday var startDay = Math.min(...daysSelected); var endDay = Math.max(...daysSelected); var datesObj = []; while (startDate <= endDate) { if (startDay <= startDate.getDay() && endDay >= startDate.getDay()) { datesObj.push(startDate.getFullYear() + "-" + (startDate.getMonth() + 01) + "-" + startDate.getDate()); } if (datesObj.length > 0 && startDate.getDay() == 6) { startDate.setDate(startDate.getDate() + (recurrencePattern - 7 + 1)); } else { startDate.setDate(startDate.getDate() + 1); } } return datesObj; } console.log(dates(new Date("2020/06/22"), new Date("2020/08/16")));

暂无
暂无

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

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