我有一个数组,其时间存储在Unix时间戳和当天播放的匹配数,
[
,
{
"time": 1332547200000,
"count": 2
},
{
"time": 1332633600000,
"count": 1
},
{
"time": 1332720000000,
"count": 4
},
{
"time": 1332806400000,
"count": 4
},
...
]
我正在尝试将其转换为日期格式为“20070101”的元素字符串,值
例如,我应该以表格"20070101, 46 \n"
+20072102, 26 \n" +
等获得输出。
答案 0 :(得分:1)
:)
function pad2(number) {
return (number < 10 ? '0' : '') + number
}
var ar = [{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }, { "time": 1332720000000, "count": 4 }];
var output = ""
for (i=0; i<ar.length; i++) {
var d = new Date(parseInt(ar[i].time));
var year = ""+d.getFullYear();
var month = pad2(d.getMonth());
var date = pad2(d.getDate());
var output = output+year+date+month+", "+ar[i].count+"\n";
}
document.write(output);
答案 1 :(得分:0)
var d = new Date(milliseconds);
var formatted = d.getFullYear()+d.getMonth()+d.getDate();
答案 2 :(得分:0)
这是你的FIDDLE
和你的代码:
var a=[
{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }, { "time": 1332720000000, "count": 4 }, { "time": 1332806400000, "count": 4 }, { "time": 1332892800000, "count": 5 }, { "time": 1332979200000, "count": 1 }, { "time": 1333065600000, "count": 1 }, { "time": 1333152000000, "count": 1 }, { "time": 1333324800000, "count": 2 }, { "time": 1333411200000, "count": 2 }, { "time": 1333497600000, "count": 3 }, { "time": 1333584000000, "count": 4 }, { "time": 1333670400000, "count": 3 }, { "time": 1333756800000, "count": 2 }, { "time": 1333843200000, "count": 2 }, { "time": 1333929600000, "count": 1 }, { "time": 1334102400000, "count": 9 }, { "time": 1334188800000, "count": 8 }, { "time": 1334275200000, "count": 6 }, { "time": 1334361600000, "count": 3 }, { "time": 1334448000000, "count": 2 }, { "time": 1334534400000, "count": 3 }, { "time": 1334620800000, "count": 3 }, { "time": 1334707200000, "count": 5 }, { "time": 1334793600000, "count": 4 }, { "time": 1334880000000, "count": 1 }, { "time": 1334966400000, "count": 5 }, { "time": 1335139200000, "count": 6 }, { "time": 1335225600000, "count": 3 }, { "time": 1335312000000, "count": 8 }, { "time": 1335398400000, "count": 8 }, { "time": 1335484800000, "count": 9 }, { "time": 1335571200000, "count": 4 }, { "time": 1338508800000, "count": 69 }, { "time": 1338595200000, "count": 30 }, { "time": 1338681600000, "count": 17 }, { "time": 1338768000000, "count": 70 }, { "time": 1338854400000, "count": 109 }, { "time": 1338940800000, "count": 80 }, { "time": 1339027200000, "count": 141 }, { "time": 1339113600000, "count": 94 }, { "time": 1339200000000, "count": 34 }, { "time": 1339286400000, "count": 26 }, { "time": 1339372800000, "count": 91 }, { "time": 1339459200000, "count": 166 }, { "time": 1339545600000, "count": 89 }, { "time": 1339632000000, "count": 121 }, { "time": 1339718400000, "count": 74 }, { "time": 1339804800000, "count": 48 }, { "time": 1339891200000, "count": 13 }, { "time": 1339977600000, "count": 58 }, { "time": 1340064000000, "count": 123 }, { "time": 1340150400000, "count": 110 }]
function getformateddate(date)
{
var date= new Date(date);
return date.getFullYear().toString()+date.getDate()+date.getMonth();
}
var finalString='';
for (var i=0;i<a.length;i++)
{
finalString+=getformateddate(a[i].time)+' '+a[i].count+" \\"+"n"
}
document.write(finalString)
答案 3 :(得分:0)
不幸的是,原始javascript中似乎没有任何日期格式化/零填充功能。我建议使用jQuery或其他库。这是原始解决方案。
var data = [
{ "time": 1332547200000, "count": 2 },
{ "time": 1332633600000, "count": 1 },
{ "time": 1332720000000, "count": 4 }
];
function pad(str, n) {
if (str.length < n) {
return pad("0" + str, n);
} else {
return str
}
}
for (var i = 0; i < data.length; i++) {
var datum = data[i];
var date = new Date(datum.time);
var str = date.getFullYear()
+ pad((date.getMonth() + 1).toString(), 2)
+ pad(date.getDate().toString(), 2)
+ ", " + datum.count + " \\n";
window.alert(str);
}
答案 4 :(得分:0)
这是一个JSON字符串,您可以在$.parseJSON
中使用jQuery
:
myJSON = $.parseJSON('[{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }]');
var newJSON = new Array();
var i = 0;
$.each(myJSON, function(count, line){
var newTime = new Date(parseInt(line.time));
newTime = newTime.getFullYear()+''+('0' + (newTime.getMonth()+1)).slice(-2)+''+('0' + newTime.getDate()).slice(-2);
$('#json').append(newTime+', '+line.count+'\n');
});