How to properly convert end_time timestamp into date

一笑奈何 提交于 2020-03-03 02:24:13

问题


I'm requesting data from Facebook Insights API with granularity set to a day, let's take likes as an example. My request goes to: PAGEID/insights/page_fan_adds_unique/day

Response looks like this:

{
  "data": [
    {
      "id": "PAGEID/insights/page_fan_adds_unique/day",
      "name": "page_fan_adds_unique",
      "period": "day",
      "values": [
        {
          "value": 3,
          "end_time": "2014-08-29T07:00:00+0000"
        },
        {
          "value": 4,
          "end_time": "2014-08-30T07:00:00+0000"
        },
        {
          "value": 1,
          "end_time": "2014-08-31T07:00:00+0000"
        }
      ],
      "title": "Daily New Likes",
      "description": "Daily: The number of new people who have liked your Page (Unique Users)"
    }
  ]
}

The problem is that the end_time values returned by Facebook are timestamps for PST midnight at the end of the day in question. So the row with timestamp of 2014-08-31T07:00:00+0000 actually shows data for 2014-08-30 (which is consistent with Facebook Insights charts).

What I want to do is to convert the timestamp for each data row into proper date. My server is in CET so the easiest thing for me would be to just straight up subtract 10 hours from the timestamp returned by FB (so I have 23:00 in my timezone) and there I have it, but that's obviously not a good solution.

for(var i=0,j=rows.length;i<j;i++) {
    time = rows[i].end_time;
    date = moment(time).subtract("hours", 10).format("YYYY-MM-DD");
}

How to properly utilize Moment.js to do this?


回答1:


Actually, UTC-7 would be Pacific Daylight Time (PDT). Pacific Standard Time (PST) is UTC-8.

But reading the Facebook Insights documentation, in the FAQ it says:

All daily, weekly and monthly Insights data are aggregated according to PDT (Pacific Daylight Time).

That's a bit strange, as it would mean that -7 is used year-round, even when PDT is not in effect. But hey, if that's what's in the docs then I guess Facebook has their reasons.

Fortunately, this is easy to compensate for with moment.

date = moment(time).zone(7).format("YYYY-MM-DD");

For this operation, the time zone of your server or of the client does not matter.



来源:https://stackoverflow.com/questions/25625326/how-to-properly-convert-end-time-timestamp-into-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!