Question

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...

Was it helpful?

Solution

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());

OTHER TIPS

// 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

*/

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.

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)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top