正确显示Instagram日期

时间:2012-09-26 23:48:34

标签: javascript api date instagram

这是我的网络应用程序的一个脏JSFiddle版本,但是,我不明白如何从Unix TimeStamp正确调整日期,我想要显示的是月,日和年。

任何帮助都会非常感激!

http://jsfiddle.net/89YCe/

以下是代码:

    $(function() {
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
        success: function(data) {
            console.log(data);
            //OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
            for (var i = 0; i < 5; i++) {
                    $(".instagram").append("\
                        <div class='instagram-feed'>\
                            <img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
                            <div class='igHover2'>\
                                posted by: "+data.data[i].user.username+"<br />\
                                posted on: "+Date(data.data[i].created_time).toString()+"<br />\
                            </div />\
                        </div>\
                    ");
            }
        }
    });
});​

3 个答案:

答案 0 :(得分:14)

你在这里:

$(function() {
    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
        success: function(data) {
            console.log(data);
            //OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
            for (var i = 0; i < 5; i++) {
                var date = new Date(parseInt(data.data[i].created_time) * 1000);
                    $(".instagram").append("\
                        <div class='instagram-feed'>\
                            <img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
                            <div class='igHover2'>\
                                posted by: "+data.data[i].user.username+"<br />\
                                posted on: "+(date.getMonth()+1)+"/"+date.getDate()+"/"+date.getFullYear()+"<br />\
                            </div />\
                        </div>\
                    ");
                date = null;
            }
        }
    });
});

这是一个有效的实时演示http://jsfiddle.net/89YCe/2/

答案 1 :(得分:2)

在前面的回答中,我在很多情况下得到了“NaN”。

Instagram使用Unix时间戳,我发现这个脚本效果最好:

 $(function() {
$.ajax({
    type: "GET",
    dataType: "jsonp",
    cache: false,
    url: "https://api.instagram.com/v1/tags/breathadvisor/media/recent/?access_token=16741082.1b07669.121a338d0cbe4ff6a5e04543158a4f82",
    success: function(data) {
        console.log(data);
        //OKAY, now lets get to the pretty stuff, INSTAGRAM PEEKTARS.
        for (var i = 0; i < 5; i++) {
            var date = new Date(data.data[i].created_time * 1000);
            var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
            var year = date.getFullYear();
            var month = months[date.getMonth()];
            var date = date.getDate();
            var time = month+', '+ date+' '+year ;

                $(".instagram").append("\
                    <div class='instagram-feed'>\
                        <img class='instagram-image' src='" + data.data[i].images.standard_resolution.url +"' width='325px'/>\
                        <div class='igHover2'>\
                            posted by: "+data.data[i].user.username+"<br />\
                            posted on: "+time+"<br />\
                        </div />\
                    </div>\
                ");
            date = null;
        }
    }
});

});

这是一个jsfiddle:http://jsfiddle.net/jgknott/V5TCs/

答案 2 :(得分:0)

这是一个很好的angularjs工厂,我用它来显示与instagram一样的日期:

    .factory('displaydate', ['$filter', function($filter) {
    return function (date){
        // Split timestamp into [ Y, M, D, h, m, s ]

        var actiondate = new Date(parseInt(date) * 1000);
        var today = new Date();
        if(today.getDate() === actiondate.getDate() && today.getMonth() === actiondate.getMonth() && today.getYear() === actiondate.getYear()){
            var hourssince =   today.getHours() - actiondate.getHours();
            var minutessince =   today.getMinutes() - actiondate.getMinutes();
            var secondssince =   today.getSeconds() - actiondate.getSeconds();
            if(hourssince > 0){
                date = hourssince+'u';
            }else if(minutessince > 0){
                date = minutessince+'m';
            }else{
                date = secondssince+'s';
            }
        }else{
            var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
            var diffDays = Math.round(Math.abs((today.getTime() - actiondate.getTime())/(oneDay)));
            if(diffDays >= 7){
                date = Math.round(diffDays / 7)+'w';
            }else{
                if(diffDays == '0'){
                    diffDays = '1';
                }
                date = diffDays+'d';
            }
        }
        return date;
    };
}])