SO Answer / Calculate the recurring dates between a range of dates in javascript
Link to question in result
by Adam Mendoza
HTML
<script src="http://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
var log = Helpers.log2Screen;
var DATE_FRIENDLY = 'WD MM dd yyyy';
Helpers.extendDate();
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) {
var curr = range.shift();
options.push( String.Format(optionstring,
curr.format('yyyy/mm/dd hh:mi:ss'),
...