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?