Question

I'm writing a script that's interacting with the Facebook Graph API. I managed to read the comments alright, the only thing I want to add is display the created_time field in a "2 hours ago", "4 weeks ago", etc. format.

Here is what Facebook shows me when reading the comments from a post (I'm using the https://graph.facebook.com/{post_id}/comments?access_token={...} endpoint):

{
   "data": [
      {
         "id": "730302787001258_95568060",
         "from": {
            "name": "Johanna Eris",
            "id": "100000646300114"
         },
         "message": "my comment",
         "can_remove": true,
         "created_time": "2014-03-17T11:44:39+0000",
         "like_count": 0,
         "user_likes": false
      },
   ...
}

I don't know how created_time is calculated. I noticed that the user profile (/me) has a field called timezone, but I'm not sure if this is related to the comments time. I'm wondering what happens when an user posts from a different timezone (when travelling for example), is his timezone updated?

Btw, I'm not asking how to calculate a time difference, I already know how to do that. I just want to know the timezone for comments and how it's calculated, so I can make a correct time difference.

Not sure if it's relevant, but I'm using NodeJS & the fb package.

Thanks,

Alex

Was it helpful?

Solution

Because the value contains +0000, you know that it has already been adjusted to a zero offset, which will make it equivalent to UTC. So the source time zone is not necessary.

Since you said you're using moment.js, then just pass the entire value in to the moment constructor, including the offset. It will automatically read that and adjust to local time zone.

moment("2014-03-17T11:44:39+0000")

If you don't want it adjusted, then just switch back to UTC.

moment("2014-03-17T11:44:39+0000").utc()

With either, you can use moment's fromNow function to get the string output you're looking for. You don't need to call the utc function if that's all you need:

moment("2014-03-17T11:44:39+0000").fromNow()  // "2 hours ago" (or similar)

-

As an aside - the only difference between Z and +0000 is a semantic one:

  • Z is supposed to imply that the value is in UTC, whether originating in UTC or having been converted to UTC.

  • +0000 or +00:00 is supposed to imply that the value was taken from a local time zone that is not offset from UTC at that point, for example Europe/London in the winter, or Atlantic/Reykjavik year-round.

  • And if you follow RFC3339, then there's also the possibility of -00:00, which is supposed to imply that the local value is unknown but this value is understood to be UTC.

However, they're all referring to the same point, and I doubt Facebook is trying to imply anything other than you should treat the value as UTC, so I wouldn't worry about it. ;-)

OTHER TIPS

Have a look at Moment.js, this is a javascript date library that provides this functionality plus a lot more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top