Skip to content

Instantly share code, notes, and snippets.

@ashafa
Created June 4, 2010 19:12

Revisions

  1. ashafa revised this gist Jun 4, 2010. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    /*
    * * JavaScript Pretty Date
    * * Copyright (c) 2008 John Resig (jquery.com)
    @@ -36,5 +35,5 @@

    /*
    USAGE: "<a class='time' title='" + new Date([YOUR DATE HERE]).toString("u") + "' href='#'></a>"
    NOTE: The method .toString("u") for the Date object is added my Date.JS (http://datejs.com).
    NOTE: The method .toString("u") for the Date object is added by Date.JS (http://datejs.com).
    */
  2. ashafa created this gist Jun 4, 2010.
    40 changes: 40 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@

    /*
    * * JavaScript Pretty Date
    * * Copyright (c) 2008 John Resig (jquery.com)
    * * Licensed under the MIT license.
    * */

    // Takes an ISO time and returns a string representing how
    // long ago the date represents.

    function prettyDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
    now = new Date(),
    diff = ((now.getTime() - ((date.getTime()) - (0 * 60000))) / 1000),
    day_diff = Math.floor(diff / 86400);

    return day_diff <= 0 && (
    diff < 30 && "just now" ||
    diff < 60 && "less than a minute ago" ||
    diff < 120 && "a minute ago" ||
    diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
    diff < 7200 && "about an hour ago" ||
    diff < 86400 && "about " + Math.floor( diff / 3600 ) + " hours ago") ||
    day_diff == 1 && "yesterday" ||
    day_diff + " days ago";
    };

    jQuery.fn.prettyDate = function() {
    return this.each(function() {
    var date = prettyDate(this.title);
    if ( date ) jQuery(this).text( date );
    });
    };

    $("a.time").prettyDate(); // Wrap this in a setInterval set at every 30000ms for live updates.

    /*
    USAGE: "<a class='time' title='" + new Date([YOUR DATE HERE]).toString("u") + "' href='#'></a>"
    NOTE: The method .toString("u") for the Date object is added my Date.JS (http://datejs.com).
    */