I am trying to get an array of all days in a week like today is Aug 1st so i need to get an array of this week which will look like
['2019-07-28' ,'2019-07-29', '2019-07-30', '2019-07-31', '2019-08-01', '2019-08-02', '2019-08-03']
in order to achieve this i wrote the following code
var week_list = []
const today = new Date()
week_list.push(today.toISOString().substring(0,10))
var counter = today.getDay()
// get next
var next_day = today
for(var i = counter;i<6;i++){
next_day.setDate(next_day.getDate()+1)
week_list.push(next_day.toISOString().substring(0,10))
}
// get prev
var prev_day = today
for(var i = counter;i>0;i--){
prev_day.setDate(prev_day.getDate()-1)
week_list.push(prev_day.toISOString().substring(0,10))
}
console.log(week_list)
output
[ '2019-08-01', '2019-08-02', '2019-08-03', '2019-08-02', '2019-08-01', '2019-07-31', '2019-07-30']
i am getting a different output instead of what i was expecting, the variable today change automatically when i try to update the next_day variable. Is this common under javascript else am i doing anything wrong.