Skip to content

Instantly share code, notes, and snippets.

@kgn
Created March 4, 2011 20:24
Show Gist options
  • Save kgn/855645 to your computer and use it in GitHub Desktop.
Save kgn/855645 to your computer and use it in GitHub Desktop.
Nice date delta string: 3 minutes ago, 1 hour ago, 16 days ago
// modified from: http://ejohn.org/blog/javascript-pretty-date/
(function($){
$.prettyDate = function(time){
var date = new Date((time || ''));
var diff = (((new Date()).getTime() - date.getTime())/1000);
var day_diff = Math.floor(diff / 86400);
if(diff === 0 || isNaN(day_diff) || day_diff < 0){
return;
}
//display the hours up to 48, then start counting days after 2
//days is the final interval
return (
diff < 1 && '1 second ago' ||
diff < 60 && Math.floor(diff) + ' seconds ago' ||
diff < 120 && '1 minute ago' ||
diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
diff < 7200 && '1 hour ago' ||
diff < 172800 && Math.floor( diff / 3600 ) + ' hours ago'
) || day_diff >= 2 && (
day_diff + ' days ago'
);
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment