Capture every Friday, Sunday and Wednesday between two dates

Goodnight. I’m trying to capture every Friday, Sunday and Wednesday between two dates using Moment.js. I couldn’t understand why it doesn’t capture the days:

  • 2021-12-08
  • 2021-12-10

I managed to get this far:

const allDays = [0, 3, 5];

function formatToPush(dt_inicio, dt_final, dia, horas) {

    let start = moment(dt_inicio);
    let end = moment(dt_final);

    let result = [];
    let datas = [];
    let current = start.clone();

    if (( || ( || ( + dia).isSameOrBefore(end))) {
    } => { => {
            m.set({ hour: h.split(':')[0], minute: h.split(':')[1], second: 0, millisecond: 0 });
            datas.push(m.format('YYYY-MM-DD HH:mm:ss'))

    return datas;

let final = [];

for (let i in allDays) {
    final.push(...formatToPush('2021-12-01', '2021-12-10', allDays[i], ["10:00", "16:00", "22:30"]))

<script src=""></script>

Can anyone help me find the error?



The condition for whether the day is within the bounds is always true for either or both of the first two clauses. This means the whole expression is true without evaluating the third clause which is the one that could set the date to one of the later dates you are missing. Since you aren’t running that if-statement in a loop, it will only ever push one date to the result array.

A more generalized algorithm would use a loop.

let current = start.clone();
if ( && current.isSameOrBefore(end)) {
while ( + dia).isSameOrAfter(start) && current.isSameOrBefore(end)) {

Note: I also changed the conjunction to && because with the loop, isAfter would always be true. Also I omitted in the second clause since the first one is already setting the day of the week for current.

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .