JavaScript code to display Twitter created_at as xxxx ago

前端 未结 8 494
一生所求
一生所求 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:24

    You can use prettyDate by john Resig. It has also a JQuery plugin.

    http://ejohn.org/blog/javascript-pretty-date/

    0 讨论(0)
  • 2020-12-24 15:26

    I'm using jquery timeago:

    var timeago = document.createElement('time')
    var dt = new Date(Date.parse(data['status']['created_at']));
    var datetimestr = '' + dt.getFullYear() + '-' + ("0" + (dt.getMonth() + 1)).slice(-2) + '-' + dt.getDate() + 'T' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds() + 'Z'
    $(timeago).addClass('timeago').attr('datetime', datetimestr).text(data['status']['created_at']);
    if(jQuery().timeago) {$(timeago).timeago();}
    

    More on timeago: http://timeago.yarp.com

    0 讨论(0)
  • 2020-12-24 15:30

    May be hella late but I just found out splitting the string by spaces and creating variables accordingly, easily allows you to breakdown the date.

    // Ex. Thu Sep 28 03:40:33 +0000 2017
    
    var tweetDate = data.tweet[i].created_at;
    
    tweetDate = tweetDate.split(' ');
    
    var tweetMo = tweetDate[1];
    
    0 讨论(0)
  • 2020-12-24 15:31

    From the comments, and some code from the twitter widget here is the code I came up with:

    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 "just now";}
        if (diff < 20) {return diff + " seconds ago";}
        if (diff < 40) {return "half a minute ago";}
        if (diff < 60) {return "less than a minute ago";}
        if (diff <= 90) {return "one minute ago";}
        if (diff <= 3540) {return Math.round(diff / 60) + " minutes ago";}
        if (diff <= 5400) {return "1 hour ago";}
        if (diff <= 86400) {return Math.round(diff / 3600) + " hours ago";}
        if (diff <= 129600) {return "1 day ago";}
        if (diff < 604800) {return Math.round(diff / 86400) + " days ago";}
        if (diff <= 777600) {return "1 week ago";}
        return "on " + 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:32

    Using moment.js without any plugin this is the custom format you need to use to parse the awkward Twitter date properly:

    var tweetDate = 'Mon Dec 02 23:45:49 +0000 2013';
    moment(tweetDate, 'dd MMM DD HH:mm:ss ZZ YYYY', 'en');
    
    0 讨论(0)
  • 2020-12-24 15:38
    import * as luxon from 'luxon';
    
    // https://stackoverflow.com/a/20478182/5932012
    const TWITTER_DATE_FORMAT = 'EEE MMM d HH:mm:ss ZZZ yyyy';
    
    export const parseTwitterDate = (dateStr: string): luxon.DateTime =>
        luxon.DateTime.fromString(dateStr, TWITTER_DATE_FORMAT);
    
    export const formatTwitterDate = (dateTime: luxon.DateTime): string =>
        dateTime.toFormat(TWITTER_DATE_FORMAT);
    
    0 讨论(0)
提交回复
热议问题