0

What I'm trying to do is build an array that contains the date of the prior 7 days. The code below does this for me. However, when (now.getDate() - index) is less than one, it doesn't jump the date back to the previous month, it simply brings the value negative.

I tried replacing that with (now.setDate(now.getDate() - index)) hoping to fix it, but I seem to be getting a UNIX time, and definitely not the correct one.

var bars = new Array();
    var index = 0;
    var NumFields = data.length - 2;
    var now = new Date();
    var date = new Array();

    for(var i=0;i<NumFields;i++) {
        $('.graph').append("<div class=\"bar\"></div>");
    }

    $('.graph > .bar').each(function() {
        var currentData = data[index];
        $(this).attr('value', currentData);
        bars.push(currentData);
        date.push(now.getDate() - index);
        index++;        
    });

If you want to see the problem (remember, it won't look broken because the current date minus seven days is greater than zero), then go to habitic.com and click on "Running."

Thanks for your help! I'm super confused, and this is the first problem that has stumped me enough to require asking for help...

Jerad
  • 594
  • 7
  • 15
  • Due to month and year cross-overs, this problem is a more complex than you might think it is. You have to convert to timestamp and back for this to work. I'll see if I can throw something together for you. – mVChr Oct 23 '12 at 22:20

4 Answers4

1

No, now.setDate(now.getDate() - index) actually was the rigth approach. Yet it does not return the new day, but the new [internal] timestamp of the now Date instance. Make it two steps:

now.setDate(now.getDate() - 1); // sets the date to the previous day each time
date.push(now.getDate());
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This seems to work great and I tested it with a date that rolled over into a previous month! Lastly, how do I get this to include the current day as well? So, today + the prior six days. I'm sure I could figure it out, but my brain is burnt out at the moment. – Jerad Oct 23 '12 at 23:30
  • Just push the date *before* you decrease it - think of it as a loop counter. – Bergi Oct 24 '12 at 00:26
0

// set the date first, and then push the Date object's getDate value into the array.

function pastweek(d){
    var now= d || new Date(),
    i= 6,
    dates= [now.getDate()];
    while(i--){
        now.setDate(now.getDate()-1);
        dates.push(now.getDate());
    }
    return dates.reverse();
}

/*

pastweek(new Date(2012,9,5))
returned value: (Array)
29,30,1,2,3,4,5

*/

/*

pastweek()
returned value: (Array)
17,18,19,20,21,22,23

*/

kennebec
  • 102,654
  • 32
  • 106
  • 127
-1

Try using this:

day = 24*60*60*1000
new Date(now.getTime()-index * day);

now.getTime() returns the time as the number of milliseconds since the epoch (January 1, 1970 midnight GMT). day = 24*60*60*1000 calculates the number of milliseconds in a day (24 hours, 60 minutes/hour, 60 seconds/minute, 1000 milliseconds/second). Multiply that by the offset in days (index, if I'm not mistaken), and you get the offset in milliseconds. Subtract that from getTime(), and you get the number of milliseconds since the epoch at your desired date, which you can then use in the Date() constructor to get an actual Date() object for that day and time.

FrankieTheKneeMan
  • 6,645
  • 2
  • 26
  • 37
  • Why the downvote? This is not only 100% correct, it's explained. – FrankieTheKneeMan Oct 23 '12 at 22:25
  • It's not 100% correct, e.g. it has timezone issues. Not all days are 24h long. – Bergi Oct 23 '12 at 23:33
  • @Bergi - It does not have timezone issues - all Data objects are localized just fine. And All days are indistinguishable from being 24 hours long. The Gregorian calendar (which is used and understood everywhere you can use a computer) adds a day (measured in exactly the increment above) every (not quite) four years to deal with this. While you're right, periodically, there is a leap second added, it's only going to affect this application for 7 seconds a year (on average), out of 31536000. So, I guess it's not 100% correct - just 99.99998% correct. Which is more than close enough. – FrankieTheKneeMan Oct 24 '12 at 00:37
  • 1
    see http://stackoverflow.com/q/12885743/1048572. I not (only) meant leap seconds, but there are days with 23 or 25 hours – Bergi Oct 24 '12 at 01:45
-1

I'll leave it up to you to leverage this to fit your context, but here's how you could get the last week of dates assuming today (now) is 1/3/2012:

var now = new Date(2012, 0, 3),
    DAY_MS = 86400000,  // 1 day in milliseconds
    dates = [];

for (var i = 0; i < 7; i++) {
    dates.push(new Date(now.getTime() - (i * DAY_MS)));
}

console.log(dates);

// outputs:
// [Tue Jan 03 2012 00:00:00 GMT-0800 (PST),
//  Mon Jan 02 2012 00:00:00 GMT-0800 (PST),
//  Sun Jan 01 2012 00:00:00 GMT-0800 (PST),
//  Sat Dec 31 2011 00:00:00 GMT-0800 (PST),
//  Fri Dec 30 2011 00:00:00 GMT-0800 (PST),
//  Thu Dec 29 2011 00:00:00 GMT-0800 (PST),
//  Wed Dec 28 2011 00:00:00 GMT-0800 (PST)]
mVChr
  • 49,587
  • 11
  • 107
  • 104