简体   繁体   English

使用Javascript getDate存储前一周的日期数组

[英]Storing an array of dates for the previous week in Javascript, getDate

What I'm trying to do is build an array that contains the date of the prior 7 days. 我想做的是建立一个包含前7天的日期的数组。 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. 但是,当(now.getDate()-index)小于1时,它不会将日期跳回到上个月,它只会带来负值。

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. 我试图用(now.setDate(now.getDate()-index))替换它,希望修复它,但是我似乎正在获得UNIX时间,而且绝对不是正确的时间。

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." 如果您想看到问题(请记住,由于当前日期减去7天大于零,它看起来不会损坏),然后转到habitic.com并单击“运行”。

Thanks for your help! 谢谢你的帮助! I'm super confused, and this is the first problem that has stumped me enough to require asking for help... 我非常困惑,这是第一个让我感到困扰的问题,需要寻求帮助...

No, now.setDate(now.getDate() - index) actually was the rigth approach. 不, now.setDate(now.getDate() - index)实际上是now.setDate(now.getDate() - index)的方法。 Yet it does not return the new day, but the new [internal] timestamp of the now Date instance. 但是,它不会返回新的日期,而是now Date实例的新的[内部]时间戳。 Make it two steps: 使其分为两个步骤:

now.setDate(now.getDate() - 1); // sets the date to the previous day each time
date.push(now.getDate());

// set the date first , and then push the Date object's getDate value into the array. //设置日期然后再按下日期对象的GETDATE值到阵列中。

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). now.getTime()返回自该时间点(格林尼治标准时间1970年1月1日午夜)以来的毫秒数。 day = 24*60*60*1000 calculates the number of milliseconds in a day (24 hours, 60 minutes/hour, 60 seconds/minute, 1000 milliseconds/second). day = 24*60*60*1000计算一天中的毫秒数(24小时,60分钟/小时,60秒/分钟,1000毫秒/秒)。 Multiply that by the offset in days ( index , if I'm not mistaken), and you get the offset in milliseconds. 将其乘以天数的偏移量( index ,如果我没记错的话),则得到的偏移量以毫秒为单位。 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. 从getTime()中减去该时间,您将获得自所需时间的纪元以来的毫秒数,然后可以在Date()构造函数中使用该毫秒数来获取该日期和时间的实际Date()对象。

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: 我将由您自己决定如何利用它来适应您的情况,但是如果今天( now )是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)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM