Add one day to date in javascript

后端 未结 8 1296
再見小時候
再見小時候 2020-12-14 06:00

I am sure that a lot of people asked this question but when I checked the answers it seems to me that they are wrong that what I found

var startDate = new Da         


        
相关标签:
8条回答
  • 2020-12-14 06:24

    If you don't mind using a library, DateJS (https://github.com/abritinthebay/datejs/) would make this fairly easy. You would probably be better off with one of the answers using vanilla JavaScript however, unless you're going to take advantage of some other DateJS features like parsing of unusually-formatted dates.

    If you're using DateJS a line like this should do the trick:

    Date.parse(startdate).add(1).days();
    

    You could also use MomentJS which has similar features (http://momentjs.com/), however I'm not as familiar with it.

    0 讨论(0)
  • 2020-12-14 06:26

    Just for the sake of adding functions to the Date prototype:

    In a mutable fashion / style:

    Date.prototype.addDays = function(n) {
       this.setDate(this.getDate() + n);
    };
    
    // Can call it tomorrow if you want
    Date.prototype.nextDay = function() {
       this.addDays(1);
    };
    
    Date.prototype.addMonths = function(n) {
       this.setMonth(this.getMonth() + n);
    };
    
    Date.prototype.addYears = function(n) {
       this.setFullYear(this.getFullYear() + n);
    }
    
    // etc...
    
    var currentDate = new Date();
    currentDate.nextDay();
    
    0 讨论(0)
提交回复
热议问题