简体   繁体   English

Instafeedjs moment.js返回时间之前

[英]Instafeedjs moment.js return time ago

Using instafeedjs and moment.js to render instagram feed and format the dates to a relative format ( X days/months/hours/minutes ago ). 使用instafeedjs和moment.js渲染instagram feed并将日期格式化为相对格式(X天/月/小时/分钟前)。

As you see below, the success function callback is looping thru the array to separate the images into unique DOM containers. 如下所示,成功函数回调通过数组循环以将图像分成唯一的DOM容器。 This portion works perfect, although, now that I need to obtain the 'caption.created_date', it gets a little tricky to also include. 尽管现在我需要获取“ caption.created_date”,但这一部分的效果很好,但要包含它也有些棘手。 I can output the unix timestamp pretty easily but I can't seem to translate it to the desired output, described above. 我可以很容易地输出unix时间戳,但是我似乎无法将其转换为所需的输出,如上所述。

thank you so much for help. 非常感谢您的帮助。

 var imgs = []; var feed = new Instafeed({ limit: '12', get: 'user', userId: XXXXX, clientId: 'XXXXXXXXXXXXXXXXXXXX', accessToken: 'XXXXX.XXXXX.XXXXX', limit: 20, resolution: 'standard_resolution', template: '{{model.created_time_ago}}<img src="{{image}}"/>', filter: function (image) { var imageDate = new Date(parseInt(image.created_time * 1000, 10)); var timeAgo = moment(imageDate).fromNow(); image.create_time_ago = timeAgo; return true; }, success: function (data) { // read the feed data and create owr own data struture. var images = data.data; var result; for (i = 0; i < images.length; i++) { var image = images[i]; result = this._makeTemplate(this.options.template, { model: image, id: image.id, link: image.link, caption: image.caption.text, created_time:image.caption.created_time, image: image.images[this.options.resolution].url }); imgs.push(result); } //split the feed into divs $("#gram1").html(imgs.slice(0, 1)); $("#gram2").html(imgs.slice(1, 2)); $("#gram3").html(imgs.slice(2, 3)); } }); feed.run(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script src="http://instafeedjs.com/js/instafeed.min.js"></script> <div id="gram1"></div> <div id="gram2"></div> <div id="gram3"></div> 

Ive solved this by using moment.js within the success callback right after inserting the html into the dom. 在将html插入dom之后,Ive通过在成功回调中使用moment.js解决了这一问题。 Moment is obtaining hte unix timestamp from the data attribute ive inserted into the templates paragraph. 时刻正在从插入到模板段落中的数据属性ive获取UNIX时间戳。

works like a charm! 奇迹般有效!

var imgs = [];
var feed = new Instafeed({
    limit: '12',
    get: 'user',
    userId: 1934632453,
    clientId: 'b4986d7624834f60a58c773bd4ccc5f3',
    accessToken: '1934632453.467ede5.c8d670901ebe4229b293c9ff705810d3',
    limit: 20,
    resolution: 'standard_resolution',
    template: '<p data-unix="{{created_time}}" class="unix-time"></p><img src="{{image}}"/>',
    success: function (data) {
        // read the feed data and create our own data struture.
        var images = data.data;
        var result;
        for (i = 0; i < images.length; i++) {
            var image = images[i];
            result = this._makeTemplate(this.options.template, {
                model:       image,
                id:          image.id,
                link:        image.link,
                caption:     image.caption.text,
                created_time:image.created_time,
                image: image.images[this.options.resolution].url
            });
            imgs.push(result);
        }
        //split the feed into divs
        $("#gram1").html(imgs.slice(0, 1));
        $("#gram2").html(imgs.slice(1, 2));
        $("#gram3").html(imgs.slice(2, 3));

        //convert time to relative time ago.
        $(".unix-time").each(function(){
            var time = moment.unix($(this).attr('data-unix')).fromNow();
            $(this).append(time);
        });
    }
});
feed.run();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM