Get date and month from timestamp
17,931
Solution 1
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>');
Solution 2
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"));
Solution 3
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/
Solution 4
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);
Author by
qqruza
Updated on June 04, 2022Comments
-
qqruza almost 2 years
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!
-
qqruza almost 11 yearsThank you John. Can you help me to display date and month in seperate divs please?
-
Merbin Joe almost 8 yearsIt is working with out multiply with 1000, when I multiply with 1000 it show after the 3days date.