Reputation: 1417
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:
$.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>');
}
}
});
It displays the stamp only. Can you help me please?
Also if you can do it on jsfiddle I would really appreciate it!
Upvotes: 2
Views: 18289
Reputation: 81
I know it is late but i faced this problem and i found a solution for it.
Try instead using moment()
to convert from Timestamp into actual Date
To get full date, use moment(Timestamp).format('MM/DD/YYYY')
To get month name, use moment(Timestamp).format('MMMM')
in case of your code, try the following:
$('#dates').append('<div id="date">' +
moment(item.date).format('MM/DD/YYYY') +
'</div><div id="month">' +
moment(item.date).format('MMMM') +
'</div>');
Upvotes: 0
Reputation: 1241
var dt = new Date();
var time = dt.getMonth()+1 +""+ dt.getDate()+ "" +dt.getFullYear() +""+ dt.getHours() +""+ dt.getMinutes() +""+ dt.getSeconds();
console.log(time);
Upvotes: 0
Reputation: 3311
Here is a function i wrote for this that will give you all the parameters you need and even more.
/*
* 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/
Upvotes: 2
Reputation: 354
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>');
Upvotes: 5
Reputation: 318232
item.date
is a string, and javascripts Date()
accepts numbers, so:
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);
Upvotes: 1
Reputation: 729
var date = Date.parse(item.date);
var month = date.getMonth();
var day = date.getDay();
var year = date.getYear();
Upvotes: 0
Reputation: 113385
The following function will transform it into a DDMMYY format:
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;
}
Utility:
var obj = {
"status": "ok",
"posts": [
{
"id": "21",
"title": "Title",
"date": "1374267600"
}
]
};
alert(unixEpochTime_TO_Date_DDMMYY(obj.posts[0].date, " Local"));
Upvotes: 2
Reputation: 50787
That date is actually the milliseconds since January 1st, 12:00 A.M. 1970. If you use
You can convert that milliseconds string to the date object in javascript with:
var d = new Date(item.date);
Then you can use d.toUTCString()
to get the date in UTC format.
Upvotes: 0