如何使用javascript从UNIX时间戳中获取日期?

时间:2014-08-24 21:37:03

标签: javascript datetime unix-timestamp

这是我的jsfidde:http://jsfiddle.net/9SUqX/74/

原始代码:

var comments = [
    {name:"Full Name",comment:"Quote",timestamp:1332045720},
];

$(document).ready(function() {
    $("ul#feed").append("<li>\
    <blockquote>"+comments[0].comment+"</blo‌ckquote>\
    <cite>-"+comments[0].name+", "+comments[0].timestamp+" </cite>\
    </li>");
});

1 个答案:

答案 0 :(得分:0)

您需要将时间戳乘以1000(Javascript使用毫秒)并将其传递给日期:

var comments = [
    {name:"Full Name",comment:"Quote",timestamp:1332045720},
];

$(document).ready(function() {
    var d = new Date(comments[0].timestamp * 1000);
    var date = [(d.getMonth() + 1), d.getDate(), d.getFullYear()].join("/");
    $("ul#feed").append("<li>\
    <blockquote>"+comments[0].comment+"</blo‌ckquote>\
    <cite>-"+comments[0].name+", "+date+" </cite>\
    </li>");
});