Convert date format to facebook in javascript

Hi, I am getting events from facebook using fql and javascript sdk.

How can I convert facebook format date to javascript date?

thanks

+4
source share
5 answers

Dates in javascript are numeric 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 the code for doing this for posting on my wall earlier ( 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 dates: http://momentjs.com/

+6
source

to change the api schedule "created_time" to the "previous" date to match facebook. I found a useful link for javascript / jquery: http://ejohn.org/blog/javascript-pretty-date/ p>

 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"; } 
+6
source

According to what I read here regarding the fql date format, you can get it in the format "mm / dd / yyyy" (long_numeric).

I assume that you can use 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.

+1
source

add jquery ui plugin to your page.

 function DateFormate(dateFormate, dateTime) { return $.datepicker.formatDate(dateFormate, dateTime); }; 
0
source

The date format in the Facebook format is by default "2000-01-01T00: 00: 59 + 0000", but if you specify date_format=U in the request, the format will be a single integer unix time (for example, 1428093522).

Then, just run Javascript with new Date(unix_timestamp*1000) ;

0
source

Source: https://habr.com/ru/post/1309031/


All Articles