Created
June 4, 2010 19:12
-
-
Save ashafa/425822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* * 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 by Date.JS (http://datejs.com). | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment