Reputation: 229
I have an array which has time stored in Unix timestamp and number of matches played on that day,
[
,
{
"time": 1332547200000,
"count": 2
},
{
"time": 1332633600000,
"count": 1
},
{
"time": 1332720000000,
"count": 4
},
{
"time": 1332806400000,
"count": 4
},
...
]
I am trying to convert this into a string of elements with date format "20070101", value
for example, I should get the output in the form, "20070101, 46 \n"
+20072102, 26 \n" +
etc....
Upvotes: 2
Views: 364
Reputation: 2377
Here is Your FIDDLE
AND Your Code:
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)
Upvotes: 0
Reputation: 39704
That is a JSON string and you can use $.parseJSON
in 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');
});
Upvotes: 0
Reputation: 36703
Unfortunately there doesn't seem to be any date formatting/zero padding functions in raw javascript. I'd recommend using jQuery or another library. This is the raw solution.
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);
}
Upvotes: 0
Reputation: 4947
:)
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);
Upvotes: 1
Reputation: 3949
var d = new Date(milliseconds);
var formatted = d.getFullYear()+d.getMonth()+d.getDate();
Upvotes: 0