使用JavaScript创建一天中的小时数组

时间:2014-09-30 11:01:26

标签: javascript date

我想在JavaScript中创建这样的东西

var date = {
    "29-Sept-2014" : ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"], //yesterday
    "30-Sept-2014" : ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"], //today
    "01-Oct-2014" : ["00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00"] //tomorrow
};

结构是,我希望一天中的所有时间都在一个阵列中三天。昨天,当前日期和明天..

我想要实现的目标 -

我想创建一个表格,其中包含3天(昨天,今天和明天)的小时列。

  

完成类似的事情 - 达到目的。无论如何,谢谢你们: - )

http://jsfiddle.net/rahulrulez/n8egxo2d/3/

代码段 -



var date = {}, newDate = new Date();
for (j = -1; j < 2; j++) {
    var tempDate = new Date(), tempHourArr = [];
    tempDate.setDate(tempDate.getDate() - j);
    console.log(tempDate);
    for (i = 0; i < 24; i++) {
        var hour = tempDate.setHours(i),
            minutes = tempDate.setMinutes(0),
            stringHour = (tempDate.getHours() < 10 ? '0' : '') +  
                         tempDate.getHours() + ":" + 
                         (tempDate.getMinutes() < 10 ?  '0' : '') + 
                         tempDate.getMinutes();
        tempHourArr.push(stringHour);
    }
    console.log(Date.create(tempDate).short());
    date[Date.create(tempDate).short()] = tempHourArr;
}

$("#codeHere").html(JSON.stringify(date, null, 2));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sugar/1.4.1/sugar-full.min.js"></script>
<pre id="codeHere">

</pre>
&#13;
&#13;
&#13;

我对如何使用JavaScript代码实现这一点没有任何想法..任何想法?

0 个答案:

没有答案