jquery当前时间戳减去一天(重复30天)

时间:2014-07-16 06:44:11

标签: javascript jquery

我今晚制作了这个功能,为演示图表页面创建随机数据。我想为过去30天的每一天创建随机数据。

我的思维过程是获取当前的UTC时间戳,然后为每天的数据减去(count++ * 3600000)。一切正常,但我的时间戳永远不会改变,所以每天的数据都是当天的。想法?

//current timestamp
var curr_date = $.now();

//random data generator up to 300
function randomData(label, color) {

    //counter
    var count = 0;

    //rand value
    function randValue() {
        return Math.floor(Math.random() * 300);
    }

    series = [
        {label: label, color: color,
        data: [
            [curr_date - (count * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()],
            [curr_date - (count++ * 3600000), randValue()]
        ]}
    ];

    return series;
}

2 个答案:

答案 0 :(得分:3)

我的大脑必须被炸 - 86400000是js中一天的价值。

答案 1 :(得分:0)

我在这里做了一个解决方法:JsFiddle

//current timestamp
var curr_date = $.now();

//random data generator up to 300
function randomData(label, color) {

    //counter
    var count = 0;

    //rand value
    function randValue() {
        return Math.floor(Math.random() * 300);
    }

//this.test=[count,count++,count++];

this.series = [
    {label: label, color: color,
    data: [
        [curr_date - (count * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()],
        [curr_date - (count++ * 3600000), randValue()]
    ]}
];
}

var randomData=new randomData('label','color');
console.log(randomData.series);