Search code examples
datemomentjsrecurrencemoment-recur

Monthly & bi-monthly recurrence on same weekdays using moment-recur


I'm totally new to moment.js. I just need little help for generating recurring dates. I read the documentation and not understand the use of every() function. In documentation they explained the use of every() with only start date. But how it will work for the monthly and bi-monthly recurrence on same weekdays?

I have the following code in which I provide it a start and end date, to find monthly & bi-monthly recurring dates.

  var recurrence;
  recurrence = moment().recur({
      start: baseDt,
      end: lastDt
  });

Solution

  • Instead of this you can go with something as explained below with simple JS Date() object.

    1. Find the range of dates and store them in an array.

    2. Find the week of the first date. And the day using getDay().

    3. Now loop through all your dates and check for the condition where the week and day is same. push() each date in an array. This array will contains all the dates of weekday in your given week number.

    I hope this pseudo algo will help you out in determine the monthly recurring dates on the same weekday.