views:

421

answers:

2

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

A: 

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.

Christopher Richa
+1  A: 

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:


var created_time = 1276808857;
var created_date = new Date(created_time * 1000);
var current_date = new Date();
var s = 'Post: ' + created_date.format('c') + '
Now: ' + current_date.format('c');

Result:
Post: 2010-06-17T16:07:37-05:00
Now: 2010-06-17T16:21:46-05:00

I used the code here to format the dates: http://jacwright.com/projects/javascript/date_format

Jason Goemaat