Skip to content

@jayroh /gist:425828 forked from ashafa/gist:425822
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/*
* * 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).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.