public
Created — forked from ashafa/gist:425822

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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).
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.