如何获得推文的年龄?

时间:2012-04-12 08:55:45

标签: javascript twitter

我正在使用搜索API导入一些推文并获取返回的JSON:

"results": [
    {
        "created_at": "Thu, 12 Apr 2012 04:58:01 +0000",
        "from_user": "AdamHills",
        "from_user_id": 23045431,
        "from_user_id_str": "23045431",
        "from_user_name": "Adam Hills",
        "geo": null,
        "id": 190302731255287800,
        "id_str": "190302731255287808",
        "iso_language_code": "sv",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http://a0.twimg.com/profile_images/1350305654/goteborgs-rape_normal.jpg",
        "profile_image_url_https": "https://si0.twimg.com/profile_images/1350305654/goteborgs-rape_normal.jpg",
        "source": "<a href="http://twitter.com/#!/download/iphone" rel="nofollow">Twitter for iPhone</a>",
        "text": "@Adde77 Den ska finnas tillgänglig i slutet av april.",
        "to_user": "Adde77",
        "to_user_id": 99782511,
        "to_user_id_str": "99782511",
        "to_user_name": "Adde P",
        "in_reply_to_status_id": 190222715716763650,
        "in_reply_to_status_id_str": "190222715716763648"
    }

如何获取该特定推文的年龄,以便在我的网页上打印出类似4m ago的内容?那,created_at字符串看起来很混乱!有一个简单的方法吗?

1 个答案:

答案 0 :(得分:2)

日期格式是标准格式。

只需从中构造一个Date对象,然后根据需要进行操作。

var tweet_date = new Date("Thu, 12 Apr 2012 04:58:01 +0000");