2

我很难找到仅从我的 json 时间戳中获取 DATE 和 MONTH 的解决方案。我的 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 上做到这一点,我将不胜感激!

4

8 回答 8

5

您需要先将时间戳转换为日期对象。请注意,您必须将时间戳乘以 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>');

小提琴

于 2013-07-15T19:34:00.250 回答
2

以下函数会将其转换为 DDMMYY 格式:

function unixEpochTime_TO_Date_DDMMYY (unixEpochTime, returnUTC) {
    var year, month, day;
    var dateObj = new Date (unixEpochTime * 1000);

    if (returnUTC) {
        year    = dateObj.getUTCFullYear ();
        month   = dateObj.getUTCMonth ();
        day     = dateObj.getUTCDate ();
    }
    else {
        year    = dateObj.getFullYear ();
        month   = dateObj.getMonth ();
        day     = dateObj.getDate ();
    }

    //-- Month starts with 0, not 1.  Compensate.
    month      += 1;

    /*-- Since we want DDMMYY, we need to trim the year and zero-pad
        the day and month.
        Note:  Use YYMMDD, for sorting that makes sense.
    */
    year    = (""  + year) .slice (-2);
    month   = ("0" + month).slice (-2);
    day     = ("0" + day)  .slice (-2);

    return day + month + year;
}

效用:

var obj = {
     "status": "ok",
     "posts": [
         { 
              "id": "21",
              "title": "Title",
              "date": "1374267600"
         }
    ]
};

alert(unixEpochTime_TO_Date_DDMMYY(obj.posts[0].date, " Local"));

JSFIDDLE

于 2013-07-15T19:14:17.670 回答
2

这是我为此编写的一个函数,它将为您提供所需的所有参数,甚至更多。

/*
 * This function get all date from timestamp
 */
function dataFromTimestamp(timestamp){
    var d = new Date(timestamp);

    // Time
    var h = addZero(d.getHours());              //hours
    var m = addZero(d.getMinutes());            //minutes
    var s = addZero(d.getSeconds());            //seconds

    // Date
    var da = d.getDate();                       //day
    var mon = d.getMonth() + 1;                 //month
    var yr = d.getFullYear();                   //year
    var dw = d.getDay();                        //day in week

    // Readable feilds
    months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
    var monName = months[d.getMonth()];         //month Name
    var time = h + ":" + m + ":" + s;           //full time show
    var thisDay = da + "/" + mon + "/" + yr;    //full date show

    var dateTime = {
        seconds : s,
        minutes : m,
        hours : h,
        dayInMonth : da,
        month : mon,
        year : yr,
        dayInTheWeek : dw,
        monthName : monName,
        fullTime : time,
        fullDate : thisDay
    };
    return dateTime;

    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
}

https://jsfiddle.net/mkammdgz/

于 2015-05-18T19:18:04.657 回答
1

item.date是一个字符串,并且 javascriptsDate()接受数字,所以:

var ms    = parseInt(item.posts[0].date, 10) * 1000,
    date  = new Date(ms),
    day   = date.getDate(),
    month = date.getMonth() + 1,
    elem1 = $('<div />', {id : 'date',  text : day}),
    elem2 = $('<div />', {id : 'month', text : month});

$('#dates').append(elem1, elem2);

小提琴

于 2013-07-15T19:20:31.933 回答
0

该日期实际上是自 1970 年 1 月 1 日凌晨 12:00 以来的毫秒数。如果您使用

您可以使用以下方法将该毫秒字符串转换为 javascript 中的日期对象:

var d = new Date(item.date);

然后您可以使用d.toUTCString()UTC 格式获取日期。

于 2013-07-15T19:14:15.250 回答
0
var date = Date.parse(item.date);
var month = date.getMonth();
var day = date.getDay();
var year = date.getYear();
于 2013-07-15T19:16:14.803 回答
0
  var dt = new Date();
  var time = dt.getMonth()+1 +""+ dt.getDate()+ "" +dt.getFullYear() +""+ dt.getHours() +""+ dt.getMinutes() +""+ dt.getSeconds();

  console.log(time); 
于 2016-07-01T10:39:29.917 回答
0

我知道已经晚了,但我遇到了这个问题,我找到了解决方案。

尝试使用 moment()将时间戳转换为实际日期

要获得完整日期,请使用moment(Timestamp).format('MM/DD/YYYY')

要获取月份名称,请使用moment(Timestamp).format('MMMM')

如果是您的代码,请尝试以下操作:

$('#dates').append('<div id="date">' + 
  moment(item.date).format('MM/DD/YYYY') + 
  '</div><div id="month">' + 
  moment(item.date).format('MMMM') + 
  '</div>');
于 2018-01-08T22:20:41.033 回答