0

I'm trying to get past 7 days and next 7 days date start data and end date using javascript date function.

For example : Today 31 march 2017 , When i click previous button, it will calculate from previous date ie: start and end date , (24 March 2017 to 30 March 2017) , again click previous (17 March 2017 to 23 March 2017)etcc..

Same thing will replicate for next button ..

I have tried the below things but its not working

 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;

}

Its first time comes correct 24-march 2017 to 30 march 2017 , next previous its comes 24-march to 18march2017

Any ideas ?please

1630082
  • 49
  • 3
  • 14
  • 2
    Can you use external libraries? Using moment.js for all of this sort of thing will remove many headaches and potential bugs. – Whelkaholism Mar 31 '17 at 15:46
  • If you want the 17th of March through the 23rd of March but are currently getting the 18th through the 24th, why don't you just subtract one day from your calculation? – Mike Cluck Mar 31 '17 at 15:46
  • @MikeC: I'm getting confusing the logic , thats y asking help from any one – 1630082 Mar 31 '17 at 16:03
  • @Whelkaholism: I'm suffering to write the prev next logic ..some what i have confused here – 1630082 Mar 31 '17 at 16:05
  • Where is *ProHistoryCtrl* defined? What are the values for *weekPrevcount* and *weekPrevious*? Neither function takes any arguments, nor do they return any values. Please post a minimal, working example. Calculating a date that is plus or minus a fixed number of days is answered here: [*Add +1 to current date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date). – RobG Apr 01 '17 at 08:46

2 Answers2

0

The problem seems to be with your Maths, you get the previous week by subtracting 6 for each week and then subtracting an extra 1. This is fine for the first week where -6 -1 = -7, however for the second week this is (-6 * 2) - 1 = -13 but two weeks should be -14. The code that is incorrect is:

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

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

A solution would therefore be:

 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;
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
0

Use this function. You don't need extra functions

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>
Ali
  • 1,358
  • 3
  • 21
  • 32