javascript date + 7 days

前端 未结 10 667
孤城傲影
孤城傲影 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:54

    Two problems here:

    1. seven_date is a number, not a date. 29 + 7 = 36
    2. getMonth returns a zero based index of the month. So adding one just gets you the current month number.
    0 讨论(0)
  • 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
    );
    
    0 讨论(0)
  • 2020-12-02 15:00

    var date = new Date();
    date.setDate(date.getDate() + 7);
    
    console.log(date);

    And yes, this also works if date.getDate() + 7 is greater than the last day of the month. See MDN for more information.

    0 讨论(0)
  • 2020-12-02 15:01

    You can add or increase the day of week for the following example and hope this will helpful for you.Lets see....

            //Current date
            var currentDate = new Date();
            //to set Bangladeshi date need to add hour 6           
    
            currentDate.setUTCHours(6);            
            //here 2 is day increament for the date and you can use -2 for decreament day
            currentDate.setDate(currentDate.getDate() +parseInt(2));
    
            //formatting date by mm/dd/yyyy
            var dateInmmddyyyy = currentDate.getMonth() + 1 + '/' + currentDate.getDate() + '/' + currentDate.getFullYear();           
    
    0 讨论(0)
提交回复
热议问题