Create an Array of names of last 7 days starting from today - javascript

arrays, javascript, jquery

Solution

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 
              'friday', 'saterday', 'sunday'];
var goBackDays = 7;

var today = new Date();
var daysSorted = [];

for(var i = 0; i < goBackDays; i++) {
  var newDate = new Date(today.setDate(today.getDate() - 1));
  daysSorted.push(days[newDate.getDay()]);
}

alert(daysSorted);

Problem

I'm trying to figure out, how to create a javascript array of names of ie. last 7 days starting from today. I know getDay() will return a number of the day, which I can then use as an index to access an element of an array containing days of the week. This will give me the name of today, but I need to go back chronologically to create an array of last few days I couldn't find anything similar to this problem on web. Any elegant solution for this? jQuery perhaps?

Original source

Related problems