3

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?

robjez
  • 3,740
  • 3
  • 32
  • 36

3 Answers3

7

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);
Sourabh
  • 8,243
  • 10
  • 52
  • 98
Mivaweb
  • 5,580
  • 3
  • 27
  • 53
  • I have to say I like elegancy of this solution @VDesign. I love the fact that it's extensible so I can provide numbers of days going back in time, by putting 7 into variable. A point for you mate. – robjez Nov 27 '14 at 15:21
  • I have updated the solution with a variable for the going back days – Mivaweb Nov 27 '14 at 15:25
  • awesome, that what I was looking for. – robjez Nov 27 '14 at 15:31
  • no problem if you don't understand someting from the code you can ask – Mivaweb Nov 27 '14 at 15:37
  • It's perfectly clear now. I would only move opening curly brace to the line above, to prevent javascript from screwing up something here (like automatic ending lines with semicolon), but other than that it's "clear as water" :) – robjez Nov 27 '14 at 15:47
  • don't forget to accept the answer if you think your question is solved, greetz – Mivaweb Nov 27 '14 at 15:49
  • Why make it jQuery dependant? You don't need jQuery at all. You can make it self invoking function `(function(){ })();` It's also inefficient in terms of performance, as we have and always will have 7 days in a week using Date functions inside the loop is an overkill. – Edgar Griñant Nov 27 '14 at 15:52
  • @EdgarGriñant you are right you can leave this part away, its a habbit I add this for making a solution because most of the time I use jQuery – Mivaweb Nov 27 '14 at 16:02
1
function myFunction(day) {   
   var weekday =["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
   return weekday[day];    
}
var currentDate = new Date().getDay();
var week=[0,1,2,3,4,5,6];
var a=week.splice(currentDate);
var loopWeek=a.concat(week);
var freshWeek=[]; 
for(var i=0;i<loopWeek.length;i++){
freshWeek.push(myFunction(loopWeek[i]))
}
console.log(freshWeek);
Saravanan Rajaraman
  • 1,029
  • 2
  • 12
  • 25
0

Maybe not the most elegant way:

var numdays = 7; // Change it to the number of days you need
var d = new Date();
var n = d.getDay();
var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
var myArray = new Array(numdays);
for(var i=0;i<numdays;i++){
  myArray[i]=weekday[(n-i+numdays)%7];
}
console.log(myArray); // Result:  ["Thursday", "Wednesday", "Tuesday", "Monday", "Sunday", "Saturday", "Friday"]
Edgar Griñant
  • 599
  • 1
  • 12
  • 27