Reputation: 12465
Hi i'm getting events from facebook with fql and javascript sdk.
How can i convert the facebook format date in a javascript date?
thanks
Upvotes: 4
Views: 12279
Reputation: 1933
The Facebook date format defaults to "2000-01-01T00:00:59+0000", but if you specify date_format=U
in the query, then the format is the unix timestamp integer (e.g. 1428093522).
Then, get the Javascript time simply with new Date(unix_timestamp*1000)
;
Upvotes: 1
Reputation: 29214
Dates in javascript are numeric values of milliseconds since January 1, 1970. Facebook dates (at least creation_time and update_time in the stream table) are in seconds since January 1, 1970 so you need to multiply by 1000. Here is some code doing it for a post on my wall earlier today (fiddle):
var created_time = 1276808857;
var created_date = new Date(created_time * 1000);
var current_date = new Date();
console.log('Post: ' + moment(created_date).format())
console.log('Now: ' + moment(current_date).format());
Result:
Post: 2010-06-17T16:07:37-05:00
Now: 2013-11-12T16:37:01-06:00
I used the code here to format the dates: http://momentjs.com/
Upvotes: 6
Reputation: 8552
add jquery ui plugin in your page.
function DateFormate(dateFormate, dateTime) {
return $.datepicker.formatDate(dateFormate, dateTime);
};
Upvotes: 0
Reputation: 1505
for changing the graph api "created_time" into a "ago" date to match facebook I found the fallowing link for javascript / jquery useful : http://ejohn.org/blog/javascript-pretty-date/
var created_time = "2012-04-10T00:17:46+0000";
alert(timeAgo(created_time));
function timeAgo(time){
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
Upvotes: 7
Reputation: 1295
According to what I read here concerning the fql date format, you can get it in 'mm/dd/yyyy' format (long_numeric).
I am guessing that you could use a regular expression or the following in Javascript.
var dateString = 'mm/dd/yyyy' //where 'mm/dd/yyyy' is the facebook format date
var myDate = new Date(dateString);
document.write("Date :" + myDate.getDate());
document.write("<br>");
document.write("Month : " + myDate.getMonth());
document.write("<br>");
document.write("Year : " + myDate.getFullYear());
Good luck.
Upvotes: 2