qqruza qqruza - 1 year ago 48
JSON Question

Get date and month from timestamp

I have got a problem with finding a solution on getting DATE and MONTH only from my json' timestamps. My json looks like:

"status": "ok",
"posts": [
"id": "21",
"title": "Title",
"date": "1374267600"

and ajax call:

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

It displays the stamp only. Can you help me please?

Also if you can do it on jsfiddle I would really appreciate it!

Answer Source

You need to convert the timestamp to a date object first. Note that you have to multiply timestamp with 1000 because javascript date constructor takes miliseconds while timestamp is in seconds

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