javascript date + 7 days

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

    In One line:

    new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
    
    0 讨论(0)
  • 2020-12-02 14:42

    Using the Date object's methods will could come in handy.

    e.g.:

    myDate = new Date();
    plusSeven = new Date(myDate.setDate(myDate.getDate() + 7));
    
    0 讨论(0)
  • 2020-12-02 14:47

    Without declaration

    To return timestamp

    new Date().setDate(new Date().getDate() + 7)
    

    To return date

    new Date(new Date().setDate(new Date().getDate() + 7))
    
    0 讨论(0)
  • 2020-12-02 14:47

    var future = new Date(); // get today date
    future.setDate(future.getDate() + 7); // add 7 days
    var finalDate = future.getFullYear() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+ future.getDate();
    console.log(finalDate);

    0 讨论(0)
  • 2020-12-02 14:50
    var days = 7;
    var date = new Date();
    var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    
    var d = new Date(res);
    var month = d.getMonth() + 1;
    var day = d.getDate();
    
    var output = d.getFullYear() + '/' +
        (month < 10 ? '0' : '') + month + '/' +
        (day < 10 ? '0' : '') + day;
    
    $('#txtEndDate').val(output);
    
    0 讨论(0)
  • 2020-12-02 14:54

    Something like this?

    var days = 7;
    var date = new Date();
    var res = date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    alert(res);
    

    convert to date again:

    date = new Date(res);
    alert(date)
    

    or alternatively:

    date = new Date(res);
    
    // hours part from the timestamp
    var hours = date.getHours();
    
    // minutes part from the timestamp
    var minutes = date.getMinutes();
    
    // seconds part from the timestamp
    var seconds = date.getSeconds();
    
    // will display time in 10:30:23 format
    var formattedTime = date + '-' + hours + ':' + minutes + ':' + seconds;
    alert(formattedTime)
    
    0 讨论(0)
提交回复
热议问题