获取接下来的8个星期六的日期(javascript)

时间:2014-08-06 18:45:37

标签: javascript date dayofweek getdate

我能够得到最接近星期六的日期 - CodePen

需要帮助查找接下来七个星期六的日期。 谢谢

function myFunction(x){
    var now = new Date();  
    now.setDate(now.getDate() + (x+(7-now.getDay())) % 7);
    document.getElementById("1s").innerHTML = now.getDate() + ' of ' + now.getMonth() + 'th';
}

1 个答案:

答案 0 :(得分:1)

只需将7天添加到您获得的日期并循环播放所需的周数。

function myFunction(x){

    var now = new Date();  

    now.setDate(now.getDate() + (x+(7-now.getDay())) % 7);
    document.getElementById("1s").innerHTML = now.getDate() + ' of ' + now.getMonth() + 'th';

    var newDate = now;   
    for (var i = 2; i <= 8; i++) {

      var numberOfDaysToAdd = 7;
        newDate.setDate(newDate.getDate() + numberOfDaysToAdd); 

      document.getElementById("" + i + "s").innerHTML = newDate.getDate() + ' of ' + newDate.getMonth() + 'th';
    }

}

<强> Forked codepen