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). I am able to get date object for 1 week. But I am not able to get the date object for more than a week(14 or 21 days). 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
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.
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. You need to subtract 7
from recurrencePattern
because you are already processing for one week
with loop
. Also use datesObj.length > 0
so it will only skip weeks
when dates
are actually
added for respective week.
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];
include all days between 0
to 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")));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.