Nodejs Date Add DateAdd(strInterval, Number)

Here you can find the source of DateAdd(strInterval, Number)

Method Source Code

Date.prototype.DateAdd = function(strInterval, Number) {
   var dtTmp = this;
   switch (strInterval) {
   case 's'://from www  .  j  a  va  2  s.  c o m
      return new Date(Date.parse(dtTmp) + (1000 * Number));

   case 'n':
      return new Date(Date.parse(dtTmp) + (60000 * Number));

   case 'h':
      return new Date(Date.parse(dtTmp) + (3600000 * Number));

   case 'd':
      return new Date(dtTmp.getFullYear(), (dtTmp.getMonth() + 1), dtTmp
            .getDate()
            + Number, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
            .getSeconds());
   case 'w':
      return new Date(dtTmp.getFullYear(), (dtTmp.getMonth() + 1), dtTmp
            .getDate()
            + (Number * 7), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
            .getSeconds());
   case 'q':
      return new Date(dtTmp.getFullYear(), (dtTmp.getMonth() + 1) + Number
            * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
            dtTmp.getSeconds());

   case 'm':
      return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp
            .getDate() - 1, dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
            .getSeconds());
   case 'y':
      return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp
            .getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp
            .getSeconds());
   }

}

Related

  1. add(amount,field)
    Date.prototype.add=function(amount,field){
      if(amount){
        switch (field)
          case "y":
            this.setFullYear(this.getFullYear() + amount);
            break;
          case "q":
            this.setMonth(this.getMonth() + amount * 3);
    ...
    
  2. add(interval, value)
    Date.MILLI = 'ms';
    Date.SECOND = 's';
    Date.MINUTE = 'mi';
    Date.HOUR = 'h';
    Date.DAY = 'd';
    Date.MONTH = 'mo';
    Date.YEAR = 'y';
    Date.prototype.add = function(interval, value){
      var d = this.clone();
    ...
    
  3. 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; }};
    };
    
  4. 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("");
    ...