JavaScript code to display Twitter created_at as xxxx ago

前端 未结 8 495
一生所求
一生所求 2020-12-24 14:51

I need some JS code that will take the created_at value from a Twitter feed and display it as xxxx ago.

I can find examples of creating the xxxx a

相关标签:
8条回答
  • 2020-12-24 15:44

    Here's a french language translation of Brady's solution:

    function parseTwitterDate(tdate) {
        var system_date = new Date(Date.parse(tdate));
        var user_date = new Date();
        if (K.ie) {
            system_date = Date.parse(tdate.replace(/( \+)/, ' UTC$1'))
        }
        var diff = Math.floor((user_date - system_date) / 1000);
        if (diff <= 1) {return "à l'instant";}
        if (diff < 20) {return "il y a " + diff + " secondes";}
        if (diff < 40) {return "il y a une minute";}
        if (diff < 60) {return "il y a moins d'une minute";}
        if (diff <= 90) {return "il y a une minute";}
        if (diff <= 3540) {return "il y a " + Math.round(diff / 60) + " minutes";}
        if (diff <= 5400) {return "il y a 1 heure";}
        if (diff <= 86400) {return "il y a " + Math.round(diff / 3600) + " heures";}
        if (diff <= 129600) {return "il y a 1 jour";}
        if (diff < 604800) {return "il y a " + Math.round(diff / 86400) + " jours";}
        if (diff <= 777600) {return "il y a 1 semaine";}
        return system_date;
    }
    
    // from http://widgets.twimg.com/j/1/widget.js
    var K = function () {
        var a = navigator.userAgent;
        return {
            ie: a.match(/MSIE\s([^;]*)/)
        }
    }();
    
    0 讨论(0)
  • 2020-12-24 15:49

    I was running into this issue myself, and I think it's time for a grown-up library. I use moment.js for time formatting, so I wrote an extension to do the right display parsing for Twitter:

    https://github.com/hijonathan/moment.twitter

    It also has a different method for returning super abbreviated timestamps (e.g. 7h) like in the Twitter iPhone app.

    0 讨论(0)
提交回复
热议问题