从时间戳获取日期和月份

时间:2013-07-15 19:09:13

标签: jquery json timestamp

我遇到了一个问题,就是从我的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上做,我真的很感激!

8 个答案:

答案 0 :(得分: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>');

Fiddle

答案 1 :(得分: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

答案 2 :(得分: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/

答案 3 :(得分:1)

item.date是一个字符串,javascripts Date()接受数字,所以:

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);

FIDDLE

答案 4 :(得分:0)

该日期实际上是自1月1日凌晨12点以来的毫秒数。 1970.如果你使用

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

var d = new Date(item.date);

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

答案 5 :(得分:0)

var date = Date.parse(item.date);
var month = date.getMonth();
var day = date.getDay();
var year = date.getYear();

答案 6 :(得分:0)

  var dt = new Date();
  var time = dt.getMonth()+1 +""+ dt.getDate()+ "" +dt.getFullYear() +""+ dt.getHours() +""+ dt.getMinutes() +""+ dt.getSeconds();

  console.log(time); 

答案 7 :(得分: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>');