如何正确将end_time时间戳转换为日期 [英] How to properly convert end_time timestamp into date

查看:717
本文介绍了如何正确将end_time时间戳转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Facebook Insights API请求数据,其粒度设置为一天,下面以赞为例.我的要求是: PAGEID/insights/page_fan_adds_unique/day

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

响应看起来像这样:

{
  "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)"
    }
  ]
}

问题在于,Facebook返回的end_time值是相关日期结束时PST午夜的时间戳.因此,时间戳为2014-08-31T07:00:00+0000的行实际上显示的是 2014-08-30 的数据(即与Facebook Insights图表一致).

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).

我要做的是将每个数据行的时间戳转换为正确的日期.我的服务器在CET中,所以对我来说,最简单的方法是直接从FB返回的时间戳中减去10小时(所以我在时区中有23:00),但是我知道,但这显然不是一个好的解决方案

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");
}

如何正确利用Moment.js做到这一点?

How to properly utilize Moment.js to do this?

推荐答案

实际上,UTC-7是太平洋夏令时间(PDT).太平洋标准时间(PST)为UTC-8.

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

但是请阅读FAQ中的 Facebook Insights文档:

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

所有每日,每周和每月的数据数据均根据太平洋夏令时(PDT)汇总.

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

这有点奇怪,因为这意味着全年使用-7,即使PDT无效.但是,嘿,如果这就是文档中的内容,那么我猜Facebook是有其原因的.

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.

这篇关于如何正确将end_time时间戳转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆