[英]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 week
即startDate.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
.包括0
到4
之间的所有天数。
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.