-4

I'm trying to get the previous 8 Sundays in JavaScript. Not only did this not work, but it looks very clunky. I feel something with arrays would work cleaner. What did I do wrong?

var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var lastSunday = new Date(today.setDate(today.getDate() - today.getDay()));
lastSunday = lastSunday.format("mm/dd/yyyy");

var lastSunday2 = new Date(today.setDate(today.getDate() - today.getDay() - 7));
lastSunday2 = lastSunday2.format("mm/dd/yyyy");
var lastSunday3 = new Date(today.setDate(today.getDate() - today.getDay() - 14));
lastSunday3 = lastSunday3.format("mm/dd/yyyy");
var lastSunday4 = new Date(today.setDate(today.getDate() - today.getDay() - 21));
lastSunday4 = lastSunday4.format("mm/dd/yyyy");
var lastSunday5 = new Date(today.setDate(today.getDate() - today.getDay() - 28));
lastSunday5 = lastSunday5.format("mm/dd/yyyy");
var lastSunday6 = new Date(today.setDate(today.getDate() - today.getDay() - 35));
lastSunday6 = lastSunday6.format("mm/dd/yyyy");
var lastSunday7 = new Date(today.setDate(today.getDate() - today.getDay() - 42));
lastSunday7 = lastSunday7.format("mm/dd/yyyy");
var lastSunday8 = new Date(today.setDate(today.getDate() - today.getDay() - 49));
lastSunday8 = lastSunday8.format("mm/dd/yyyy");

console.log(lastSunday8); //this gives 06/25/2017 Obviously not what I wanted.
user4020527
  • 1
  • 8
  • 20
Dong
  • 328
  • 1
  • 3
  • 16

3 Answers3

4

No need to create individual variables for each date, you can use a for loop and an array to store them.

var date = new Date();
date.setDate(date.getDate() - date.getDay()); //start at last sunday

var dates = [];
for (var i = 0; i < 8; i++) {
    dates.push(new Date(date));
    date.setDate(date.getDate() - 7); //subtract a week
}
user4020527
  • 1
  • 8
  • 20
  • This works nicely but it gives me the Mondays instead of Sundays. `var now = new Date();` `var date = new Date(now.getFullYear(), now.getMonth(), now.getDate());` Added this and it gives me Sundays, thank you! – Dong Jan 12 '18 at 02:40
  • @Dong Changing it to `date.setUTCDate(date.getUTCDate() - date.getUTCDay());` also fixed it for me, seems to be an issue with UTC/locale being mixed causing it to function incorrectly after the end of a UTC day. – user4020527 Jan 12 '18 at 02:48
1

As example - calculate time delta in milliseconds

var today = new Date();
var lastSunday = new Date(today - today.getDay()*3600*24*1000);
console.log('Last Sunday: ' + lastSunday)
for(var i=2; i <= 8; i++) {
  lastSunday = new Date(lastSunday - 7 * 3600*24*1000);
  console.log('Sunday ' + i + ': ' + lastSunday);
}
newman
  • 2,689
  • 15
  • 23
  • 1
    Not all days are 24 hours long where daylight saving is observed, so much better to use the *set* and *get* Date methods rather than adjusting the time value by 24hrs. – RobG Jan 11 '18 at 22:45
1

you are setting the date each time to equal the Sunday you are referencing in the line of code; so you are increasing each line the number of days you are subtracting where they should all just be 7 try changing all the "- number" to "- 7" because each line you are setting the date and subtracting 7 * n ; where n = number of weeks; You should also get used to using console.log() statements or cli debug to step through your code line by line to check the value variables currently hold.