如何在Javascript中过去7天和接下来的7天

时间:2017-03-31 15:43:33

标签: javascript date datetime days

我正在尝试使用javascript日期函数过去7天和接下来的7天日期开始数据和结束日期。

例如:今天2017年3月31日,当我点击上一个按钮时,它将从之前的日期计算即:开始和结束日期,(2017年3月24日至2017年3月30日),再次点击上一个(2017年3月17日至3月23日) 2017).. ETCC

同样的事情会复制下一个按钮..

我尝试过以下的东西,但它不起作用

 function getPreviousWeek(){
      ProHistoryCtrl.weekPrevcount = ProHistoryCtrl.weekPrevcount + 1;
      ProHistoryCtrl.weekPrevious  = (-6 * ProHistoryCtrl.weekPrevcount);
      getByWeek();
}

 function getNextWeek(){
      ProHistoryCtrl.weekPrevcount = ProHistoryCtrl.weekPrevcount - 1;
      ProHistoryCtrl.weekPrevious  = (-6 * ProHistoryCtrl.weekPrevcount);
      getByWeek();
}


function getByWeek(){   

      console.log("weekpreviouscount" + ProHistoryCtrl.weekPrevcount); //-6,-12,-18,-24
      console.log("weekprevious" + ProHistoryCtrl.weekPrevious); //-6,-12,-18,-24
      var d2 = new Date(); // 31.01.2017
      var d1 = new Date(d2);
      d1.setDate(d2.getDate() - 1); // 30.01.2017
      var previousWeek = '';
      var current_day = '';

      console.log("d2date" + d1.getDate());
      console.log("week2" + ProHistoryCtrl.weekPrevious)
      previousWeek = new Date(d1);
      previousWeek.setDate(d1.getDate() + ProHistoryCtrl.weekPrevious); // 30-13=17

      current_day = new Date(d1); // 30.01.2017
      current_day.setDate(d1.getDate() + ProHistoryCtrl.weekPrevious + 6); // 30-12+5=23

      console.log("currentdayprevious" + ProHistoryCtrl.weekPrevious); //-6,-12,-18,-24

      var previousWeekUTCTimestamp = Math.floor(previousWeek.getTime() / 1000);
      var currentUTC = Math.floor(current_day.getTime() / 1000);

      console.log("previousWeekUTCTimestamp" + previousWeekUTCTimestamp);
      console.log("currentUTC" + currentUTC);

      var sinceUTC = previousWeekUTCTimestamp;
      var untilUTC = currentUTC;

}

第一次来到2017年3月24日至2017年3月30日,下一次是24月3日至18月20日

有什么想法吗?请

2 个答案:

答案 0 :(得分:0)

问题似乎与你的数学有关,你得到前一周减去每周6,然后减去额外的1.这对第一周来说是好的,其中-6 -1 = -7,但对于第二周这周是(-6 * 2) - 1 = -13但是两周应该是-14。不正确的代码是:

ProHistoryCtrl.weekPrevious  = (-6 * ProHistoryCtrl.weekPrevcount);

d1.setDate(d2.getDate() - 1);

因此,解决方案是:

 function getPreviousWeek(){
            ProHistoryCtrl.weekPrevcount = ProHistoryCtrl.weekPrevcount + 1;
            ProHistoryCtrl.weekPrevious  = (-7 * ProHistoryCtrl.weekPrevcount);
            getByWeek();
}

 function getNextWeek(){
            ProHistoryCtrl.weekPrevcount = ProHistoryCtrl.weekPrevcount - 1;
            ProHistoryCtrl.weekPrevious  = (-7 * ProHistoryCtrl.weekPrevcount);
            getByWeek();
}

function getByWeek(){   

    var d2 = new Date(); // 31.01.2017
    var d1 = new Date(d2);
    d1.setDate(d2.getDate()); // 30.01.2017
    var previousWeek = '';
    var current_day = '';

    previousWeek = new Date(d1);
    previousWeek.setDate(d1.getDate() + ProHistoryCtrl.weekPrevious); // 30-13=17

    current_day = new Date(d1); // 30.01.2017
    current_day.setDate(d1.getDate() + ProHistoryCtrl.weekPrevious + 6); // 30-12+5=23

    var previousWeekUTCTimestamp = Math.floor(previousWeek.getTime() / 1000);
    var currentUTC = Math.floor(current_day.getTime() / 1000);

    var sinceUTC = previousWeekUTCTimestamp;
    var untilUTC = currentUTC;
}

答案 1 :(得分:0)

使用此功能。你不需要额外的功能



var btn = document.querySelector("button")

Date.prototype.addDays = function(days) {
  this.setDate(this.getDate() + parseInt(days));
  return this;
};


function getDate(days) {
  var date = new Date().addDays(days);
  return date
}



btn.addEventListener("click", function() {
  var pastSevenDays = getDate(-7)
  var nextSevenDays = getDate(7)
  // only date
  console.log(pastSevenDays.toLocaleString().slice(0,10))
  console.log(nextSevenDays.toLocaleString().slice(0,10))
}, false)

<button>Get Dates</button>
&#13;
&#13;
&#13;