AddDays - move a date forward or backward by a number of days. - Node.js Date

Node.js examples for Date:Day

Description

AddDays - move a date forward or backward by a number of days.

Demo Code

/**//from  ww  w  . j a  v a2s  .  c o m
 * This is a set of javascript object extensions used to help
 * format data and manipulate dates.
 */


// US only extension
Date.Months = ['January', 'February', 'March', 'April',
               'May', 'June', 'July', 'August',
               'September', 'October', 'November', 'December'];
Date.Mos    = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Date.DaysOfTheWeek = ['sunday', 'monday', 'tuesday',
               'wednesday', 'thursday', 'friday', 'saturday'];
Date.DOWs   = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];



/**
 * addDays - move a date forward or backward by a number of days.
 *
 * @param {Number} days - an integer value
 * @return this
 */
Date.prototype.addDays = function (days) {
    if (isNaN(days)) {
        throw 'addDays: days "' + days + '" is not a number';
    }
    if (!isFinite(days)) {
        throw 'addDays: days "' + days + '" must be finite';
    }
    if (Math.floor(days) !== days) {
        throw 'addDays: days "' + days + '" must be an integer';
    }
    this.setDate(this.getDate() + days);
    return this;
};

/**
 * toShortdate - returns a date in Mon DD, YYYY format
 *
 * @return {String} - an abbreviated date form
 */
Date.prototype.toShortDate = function () {
    return Date.Mos[this.getMonth()] + ' ' + this.getDate() + ', ' + this.getFullYear();
};

/**
 * getDayOfWeek - Returns full day name for the current day.
 *
 * @return {String} - Sun, Mon, Tue, etc.
 */
Date.prototype.getDayOfWeek = function () {
    return Date.DaysOfTheWeek[this.getDay()].capitalize();
};

/**
 * getDOW - Returns short day name for the current day.
 *
 * @return {String} - Sun, Mon, Tue, etc.
 */
Date.prototype.getDOW = function () {
    return Date.DOWs[this.getDay()];
};

/**
 * getMonthLong - Returns full month name for the current month.
 *
 * @return {String} - January, February, March, etc.
 */
Date.prototype.getMonthLong = function () {
    return Date.Months[this.getMonth()];
};

/**
 * getMonthShort - Return the 3 letter representation of the
 * current month.
 *
 * @return {String} - Jan, Feb, Mar, etc.
 */
Date.prototype.getMonthShort = function () {
    return Date.Mos[this.getMonth()];
};

Related Tutorials