我在寻找仅从 json 时间戳获取日期和月份的解决方案时遇到问题。我的 json 看起来像:
{
"status": "ok",
"posts": [
{
"id": "21",
"title": "Title",
"date": "1374267600"
}
]
}
和ajax调用:
$.ajax({
url: ,
async: false,
callback: 'callback',
crossDomain: true,
contentType: 'application/json; charset=utf-8',
dataType: 'jsonp',
timeout: 2000,
success: function (data, status) {
if (data !== undefined && data.posts !== undefined) {
$('#dates').append('<div id="date">' + item.date + '</div><div id="month">' + item.date + '</div>');
}
}
});
它仅显示图章。你能帮我一下吗?
如果你能在 jsfiddle 上做到这一点,我将非常感激!
最佳答案
您需要先将时间戳转换为日期对象。 请注意,您必须将时间戳乘以 1000,因为 javascript 日期构造函数需要毫秒,而时间戳以秒为单位
months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
timestamp = "1374267600";
var jsDate = new Date(timestamp*1000);
$('#dates').append('<div id="date">' + jsDate.toDateString() + '</div>'+
'<div id="month">' + months[jsDate.getMonth()] + '</div>');
关于jquery - 从时间戳获取日期和月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17661959/