7

这是我的网络应用程序的肮脏 JSFiddle 版本,但是,我不明白如何从 Unix 时间戳中正确调整日期,我只想显示月、日和年。

任何帮助将不胜感激!

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

3 回答 3

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/的现场演示

于 2012-09-27T00:16:13.720 回答
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/

于 2014-06-20T03:03:39.790 回答
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;
    };
}])
于 2016-01-08T09:02:52.743 回答