Nodejs Date Add dateAdd(size,value)

Here you can find the source of dateAdd(size,value)

Method Source Code

Date.prototype.dateAdd = function(size,value) {
    value = parseInt(value);//from  w ww .ja v a2s  .  c o m
    var incr = 0;
    switch (size) {
        case 'year':
            this.setFullYear(this.getUTCFullYear()+value);
            break;
        case 'month':
            value = value + this.getUTCMonth();
            if (value/12>0) {
                this.dateAdd('year',value/12);
                value = value % 12;
            }
            this.setUTCMonth(value);
            break;
        case 'week':
            incr = value * 7;
            this.dateAdd('day',incr);
            break;
        case 'day':
            incr = value * 24;
            this.dateAdd('hour',incr);
            break;
        case 'hour':
            incr = value * 60;
            this.dateAdd('minute',incr);
            break;
        case 'minute':
            incr = value * 60;
            this.dateAdd('second',incr);
            break;
        case 'second':
            incr = value * 1000;
            this.dateAdd('millisecond',incr);
            break;
        case 'millisecond':
            this.setTime(this.getTime() + value);
            break;
        default:
            throw new Error('Invalid date increment passed');
            break;
    }
}

Related

  1. add(thisMany)
    Date.prototype.add = function(thisMany) {
      var self = this;
      return { minutes: function() {
        var newDate = new Date(self);
        newDate.setMinutes(self.getMinutes() + thisMany);
        return newDate; }};
    };
    
  2. addDate(num)
    Number.prototype.addDate=function(num){
        var date=this;
        var dates=[];
        var d=new Date(Number(date).dateFormat());
        d.setDate(d.getDate()+(num));
        dates.push(d.getFullYear());
        dates.push(Number(d.getMonth()+1).addZero());
        dates.push(Number(d.getDate()).addZero());
        var next_date=dates.join("");
    ...
    
  3. addMilliseconds(value)
    Date.prototype.addMilliseconds = function(value)
        this.setTime(this.getTime() + value);
        return this;
    };
    
  4. dateAdd(interval, number)
    Date.prototype.dateAdd = function (interval, number) {
        var d = new Date(this);
        var k = { 'y': 'FullYear', 'q': 'Month', 'm': 'Month', 'w': 'Date', 'd': 'Date', 'h': 'Hours', 'n': 'Minutes', 's': 'Seconds', 'ms': 'MilliSeconds' };
        var n = { 'q': 3, 'w': 7 };
        eval('d.set' + k[interval] + '(d.get' + k[interval] + '()+' + ((n[interval] || 1) * number) + ')');
        return d;
    
  5. dateAdd(interval, number)
    Date.prototype.dateAdd = function (interval, number) {
        var d = this;
        var k = {
            'y': 'FullYear',
            'q': 'Month',
            'm': 'Month',
            'w': 'Date',
            'd': 'Date',
            'h': 'Hours',
    ...