将日期(以毫秒为单位)转换为dd / mm / yyyy

时间:2014-11-23 07:02:54

标签: javascript jquery

我有一个jQuery表,其中的列包含以毫秒为单位的日期格式。我想从毫秒转换为mm / dd / yyyy。我还没有弄清楚如何转换日期值并将其集成到我的脚本中。

请查看我的小提琴并提供您可能提出的任何建议:http://jsfiddle.net/tommy6s/eLbq2wvh/

$(document).ready(function() {
    $.getJSON("http://www.corsproxy.com/dvl.thomascooper.com/data/json_return.json", function(data) {
        //static table head
        $('table.stats').append("<th>" + "</th>" + "<th>" + "Date" + "</th>" + "<th>" + "Brand" + "</th>" + "<th>" + "Author" + "</th>" + "<th>" + "Title" + "</th>" + "<th>" + "Posts" + "</th>" + "<th>" + "Exposure" + "</th>" + "<th>" + "Engagement" + "</th>");
        //loop through json data
        $.each(data.data.rows, function(i, val) {
            //+1 to number each row starting at 1
            var rowNum = i + 1;
            //create table rows and cell and populate with data 
            $('table.stats').append("<tr>" + "<td>" + rowNum + "</td>" + "<td>" + val[0].value + "</td>" + "<td>" + val[1].value + "</td>" + "<td>" + val[2] + "</td>" + "<td>" + val[3].label + "</td>" + "<td>" + val[4].values[0] + "</td>" + "<td>" + val[5].values[0] + "</td>" + "<td>" + val[6].values[0] + "</td>" + "</tr>");

        });
    });
});

1 个答案:

答案 0 :(得分:0)

您可以使用:

new Date(val[0].value).toLocaleFormat('%m/%d/%Y')

而不是val[0].value

请参阅this