javascript date + 7 days

前端 未结 10 697
孤城傲影
孤城傲影 2020-12-02 14:18

What\'s wrong with this script?

When I set my clock to say 29/04/2011 it adds 36/4/2011 in the week input! but the correct date should be 6/

10条回答
  •  青春惊慌失措
    2020-12-02 14:56

    The simple way to get a date x days in the future is to increment the date:

    function addDays(dateObj, numDays) {
      return dateObj.setDate(dateObj.getDate() + numDays);
    }
    

    Note that this modifies the supplied date object, e.g.

    function addDays(dateObj, numDays) {
       dateObj.setDate(dateObj.getDate() + numDays);
       return dateObj;
    }
    
    var now = new Date();
    var tomorrow = addDays(new Date(), 1);
    var nextWeek = addDays(new Date(), 7);
    
    alert(
        'Today: ' + now +
        '\nTomorrow: ' + tomorrow +
        '\nNext week: ' + nextWeek
    );
    

提交回复
热议问题