如何使用javascript / jquery增加天数

时间:2016-12-11 11:04:20

标签: javascript jquery calendar days

我有一个工作日表如下。我需要整整一周。我能够打印当前日期,月份和工作日的名称,但我不知道如何打印剩余的日子增加1天。如果你能提出建议,我将不胜感激。

     $(document).ready(function() {
        var d = new Date();
        var monthNames = ["JAN", "FEB", "NAR", "APR", "MAY", "JUNE", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
        var weekdays = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
        var currentmonth = monthNames[d.getMonth()];
        var weekday = weekdays[d.getDay()];
        var currentday = d.getDate();
        $('#currentday').text(currentday);
        $('#currentmonth').text(currentmonth);
        $('#weekday').text(weekday);
        });
    <div id="weekday">SUN</div>
    <div id="currentday ">11</div>
    <div id="currentmonth">DEC</div>
    <div>------------------------</div>
    <div id="">MON</div>
    <div id="">12</div>
    <div id="">DEC</div>

    <div id="">TUE</div>
    <div id="">13</div>
    <div id="">DEC</div>

    <div id="">WED</div>
    <div id="">14</div>
    <div id="">DEC</div>

    <div id="">THU</div>
    <div id="">15</div>
    <div id="">DEC</div>

    <div id="">FRI</div>
    <div id="">16</div>
    <div id="">DEC</div>

    <div id="">SAT</div>
    <div id="">17</div>
    <div id="">DEC</div>

1 个答案:

答案 0 :(得分:0)

试试这个会对你有所帮助。 您需要获取当前日期时间然后添加1天(1000 * 60 * 60 * 24)并重复它

$(document).ready(function() {
  var d = new Date();
  var monthNames = ["JAN", "FEB", "NAR", "APR", "MAY", "JUNE", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
  var weekdays = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];

  for (var i = 0; i < 7; i++) {
    var date = new Date(d.getTime() + ((1000 * 60 * 60 * 24) * i));
    var currentmonth = monthNames[date.getMonth()];
    var weekday = weekdays[date.getDay()];
    var currentday = date.getDate();
    var wkday = "weekday" + i;
    var currday = "currentday" + i;
    var currmnth = "currentmonth" + i;
    $('#body').append("<div id=" + wkday + "></div>");
    $('#body').append("<div id=" + currday + "></div>");
    $('#body').append("<div id=" + currmnth + "></div>");
    $('#' + currday).text(currentday);
    $('#' + currmnth).text(currentmonth);
    $('#' + wkday).text(weekday);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

<body id="body">

</body>