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