Parse Twitter created_at value into friendly time format; relative time; time ago

// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();

var H = function (a) {
    var b = new Date();
    var c = new Date(a);
    if (K.ie) {
        c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
    }
    var d = b - c;
    var e = 1000,
        minute = e * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    if (isNaN(d) || d < 0) {
        return ""
    }
    if (d < e * 7) {
        return "right now"
    }
    if (d < minute) {
        return Math.floor(d / e) + " seconds ago"
    }
    if (d < minute * 2) {
        return "about 1 minute ago"
    }
    if (d < hour) {
        return Math.floor(d / minute) + " minutes ago"
    }
    if (d < hour * 2) {
        return "about 1 hour ago"
    }
    if (d < day) {
        return Math.floor(d / hour) + " hours ago"
    }
    if (d > day && d < day * 2) {
        return "yesterday"
    }
    if (d < day * 365) {
        return Math.floor(d / day) + " days ago"
    } else {
        return "over a year ago"
    }
};

console.log(H("Wed, 08 Apr 2009 19:22:10 +0000"));
console.log(H("Thu, 20 May 2010 19:22:10 +0000"));

12 comments

  1. anonymous

    Thanks a bunch for this!

  2. anonymous

    Thank you very much

  3. anonymous
    Here's the PHP version: function ago($a) {              $b = strtotime("now");     $c = strtotime($a);     $d = $b - $c;     $minute = 60;     $hour = $minute * 60;     $day = $hour * 24;     $week = $day * 7;              if(is_numeric($d) && $d > 0) {                  if($d < 3) return "right now";         if($d < $minute) return floor($d) . " seconds ago";         if($d < $minute * 2) return "about 1 minute ago";         if($d < $hour) return floor($d / $minute) . " minutes ago";         if($d < $hour * 2) return "about 1 hour ago";         if($d < $day) return floor($d / $hour) . " hours ago";         if($d > $day && $d < $day * 2) return "yesterday";         if($d < $day * 365) return floor($d / $day) . " days ago";                  return "over a year ago";     } }
  4. anonymous

    Sorry Im not sure exactly how to use this?

  5. anonymous

    to use:

    created_at = "Wed, 08 Apr 2009 19:22:10 +0000";

    alert(H(created_at));

  6. anonymous

    thank u sir

  7. anonymous

    Thank you!

  8. anonymous

    Gracias!

  9. anonymous
    Compiled JavaScript Code has been changed: var X = function () {     var Z = navigator.userAgent;     return {         ie: Z.match(/MSIE\s([^;]*)/)     } }(); var I = function (f) {     var h = new Date();     var d = new Date(f);     if (X.ie) {         d = Date.parse(f.replace(/( \+)/, " UTC$1"))     }     var g = h - d;     var a = 1000,         b = a * 60,         c = b * 60,         e = c * 24,         Z = e * 7;     if (isNaN(g) || g < 0) {         return ""     }     if (g < a * 7) {         return "right now"     }     if (g < b) {         return Math.floor(g / a) + " seconds ago"     }     if (g < b * 2) {         return "about 1 minute ago"     }     if (g < c) {         return Math.floor(g / b) + " minutes ago"     }     if (g < c * 2) {         return "about 1 hour ago"     }     if (g < e) {         return Math.floor(g / c) + " hours ago"     }     if (g > e && g < e * 2) {         return "yesterday"     }     if (g < e * 365) {         return Math.floor(g / e) + " days ago"     } else {         return "over a year ago"     } };
  10. anonymous

    Last work well in explorer thanks!!!

  11. anonymous

    Thank you!!

  12. anonymous

    for anyone still looking for a JavaScript implementation, use can now use Twitter's twitter-cldr-js.

    see the TwitterCldr.TimespanFormatter / Relative Dates and Times.

    https://github.com/twitter/twitter-cldr-js

Leave a Reply