Looping through dates by step - Node.js Date

Node.js examples for Date:Date Calculation

Description

Looping through dates by step

Demo Code


// from http://trentrichardson.com/2011/09/29/looping-through-dates-with-javascript/
Date.prototype.each = function (endDate, part, step, fn, bind) {
    var fromDate = new Date(this.getTime()),
        toDate = new Date(endDate.getTime()),
        pm = fromDate <= toDate ? 1 : -1,
        i = 0;/* ww  w.  ja  v  a 2 s  .  c om*/
    while ((pm === 1 && fromDate <= toDate) || (pm === -1 && fromDate >= toDate)) {
        if (fn.call(bind, fromDate, i, this) === false) break;
        i += step;
        fromDate.adjust(part, step * pm);
    }
    return this;
};

Related Tutorials