简体   繁体   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

I need to return some date object which occurs between Start Date and End Date with recurrence pattern and given weekdays using pure javaScript(No postman or moment).我需要返回一些日期 object,它发生在开始日期和结束日期之间,具有重复模式,并使用纯 javaScript(无 postman 或时刻)给出工作日。 I am able to get date object for 1 week.我能够获得 1 周的日期 object。 But I am not able to get the date object for more than a week(14 or 21 days).但我无法获得日期 object 超过一周(14 或 21 天)。 Here is my below code.这是我的以下代码。

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

the output I should get (every 2 weeks, selected Sunday and Monday fall between 22/06/2020 and 16/08/2020) is我应该得到的 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"]

but I am getting all the Sunday and Monday dates to fall from the start and end date.但我让所有星期日和星期一的日期都从开始日期和结束日期开始。 Can anyone please fix this issue.任何人都可以解决这个问题。

You can update your while(i<daysSelected.length){... } code with below code.您可以使用以下代码更新您的while(i<daysSelected.length){... }代码。

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

Moreover to skip recurrencePattern = 14 days when you encounter last day of week which is startDate.getDay() == 6 then skip recurrencePattern-7+1 days.此外,当您遇到last day of weekstartDate.getDay() == 6时,要跳过recurrencePattern = 14天,然后跳过recurrencePattern-7+1天。 You need to subtract 7 from recurrencePattern because you are already processing for one week with loop .您需要从recurrencePattern中减去7 ,因为您已经使用loop处理了one week Also use datesObj.length > 0 so it will only skip weeks when dates are actually added for respective week.还使用datesObj.length > 0因此它只会跳过actually为相应weeks添加dates的周。

Below is complete code.下面是完整的代码。 you can check it.你可以检查一下。

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

Update For var daysSelected = [0, 4];更新var daysSelected = [0, 4]; include all days between 0 to 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.

相关问题 使用纯JavaScript获取下周的开始和结束日期 - Get next week's start and end date using pure javascript 如何获取开始日期和结束日期之间的日期,结果日期应采用数组格式,使用 javascript - How to get dates that are between Start date and End date and result date should be in an array format, using javascript 如何使用JavaScript从日期选择器上的选定日期获取星期几 - How to get day of the week from the selected date on datepicker using javascript 如何使用javascript获取给定开始日期和结束日期之间的所有日期? - How to get all dates between given start and end date using javascript? 如何在 JavaScript 的日期范围之间获得重复的星期日期计数? - How to get repeating week dates count between a date range in JavaScript? 在ExtJS中获取开始/结束日期之间的日期 - Get the dates between start/end date in ExtJS 如果我在JavaScript或jquery中有开始日期,如何显示一周中所有日期的日期? - How Do I display dates of all days of week if I have a start date in JavaScript or jquery? 使用javascript显示今天日期之前3天的星期几 - displaying the day of the week 3 days before today's date using javascript 如何使用Javascript在开始日期和结束日期之间中断数据 - How to break the data in between start date and end date using Javascript JavaScript 中是否有一种方法可以根据开始日期和天数获取结束日期? - Is there a method in JavaScript to get End date based on Start date and Number of days?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM