2

Unixタイムスタンプに保存された時刻と、その日にプレイされた試合数を含む配列があります 。 [
{ "time": 1332547200000, "count": 2 }, { "time": 1332633600000, "count": 1 }, { "time": 1332720000000, "count": 4 }, { "time": 1332806400000, "count": 4 }, ... ]

これを日付形式「20070101」の要素の文字列に変換しようとしています。たとえば、値は次の形式で出力する必要があり"20070101, 46 \n" +20072102, 26 \n" +ます。

4

5 に答える 5

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);
于 2012-08-30T11:36:13.280 に答える
0
var d = new Date(milliseconds);
var formatted = d.getFullYear()+d.getMonth()+d.getDate();
于 2012-08-30T11:12:49.037 に答える
0

これがあなたのフィドルです

フィドル

そしてあなたのコード:

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)
于 2012-08-30T11:17:22.570 に答える
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);
}​
于 2012-08-30T11:28:50.970 に答える
0

$.parseJSONこれはJSON文字列であり、次の場所で使用できます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');
});

デモJSFIDDLE

于 2012-08-30T11:46:57.670 に答える