SO Answer / Calculate the recurring dates between a range of dates in javascript

Link to question in result

by KooiInc MeHere

HTML

<script src="https://kooiinc.github.io/JSHelpers/Helpers-min.js"></script>
<div class="solink" data-linkid="27763086"></div>

JavaScript

// Helpers is a small utility library
// see https://github.com/KooiInc/JSHelpers
const log = Helpers.log2Screen;
const DATE_FRIENDLY = 'WD MM dd yyyy';
Helpers.extendDate(`EN`);

log('<h3>Interval every year until 2030/1/1, no weekends</h3><hr>')
log(recurringDates(new Date('2015/1/1'),
    new Date('2030/1/1'),
    1, 'FullYear', true)
  .map(function(v) {
    return v.format(DATE_FRIENDLY);
  })
  .join('<br>'));

log('<hr><h3>interval every month until 2016/1/1, weekends allowed</h3><hr>');
log(recurringDates(new Date('2015/1/1'),
    new Date('2016/1/1'),
    1, 'Month')
  .map(function(v) {
    return v.format(DATE_FRIENDLY);
  })
  .join('<br>'));

log('<hr><h3>interval every 2 months until 2016/1/1, weekends allowed</h3><hr>');
log(recurringDates(new Date('2015/1/1'),
    new Date('2016/1/1'),
    2, 'Month')
  .map(function(v) {
    return v.format(DATE_FRIENDLY);
  })
  .join('<br>'));

log('<hr><h3>interval every 20 days until 2016/1/1, no weekends</h3><hr>');
log(recurringDates(new Date('2015/1/1'),
    new Date('2016/1/1'),
    20, 'Date', true)
  .map(function(v) {
    return v.format(DATE_FRIENDLY);
  })
  .join('<br>'));

// create a select element from a recurrent range (every 20 days until 2015/1/1)
var selectRange = document.createElement('select');
var optionstring = '<option value="{0}">{1}</option>';
var options = [String.Format(optionstring, '-1', 'select a date')];
var range = recurringDates(new Date('2015/1/1'),
  new Date('2016/1/1'),
  20, 'Date', true);
while (range.length) {
  const curr = range.shift();
  options.push(String.Format(optionstring,
    curr.format('yyyy/mm/dd hh:mi:ss'),
    curr.format(DATE_FRIENDLY)));
}
selectRange.innerHTML = options.join('');
document.querySelector('#result').insertAdjacentHTML(`beforebegin`, `
  ${selectRange.outerHTML} <span>
     from 2015/01/01 to 2016/01/01, every 20 days, no weekends</span>`);

function recurringDates(startDate, endDate, interval, intervalType, noweekends) {
  intervalType =...