Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 23rd, 2012  |  syntax: None  |  size: 1.14 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * JavaScript Pretty Date
  3.  * Copyright (c) 2011 John Resig (ejohn.org)
  4.  * Licensed under the MIT and GPL licenses.
  5.  */
  6.  
  7. // Takes an ISO time and returns a string representing how
  8. // long ago the date represents.
  9. function prettyDate(time){
  10.         var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
  11.                 diff = (((new Date()).getTime() - date.getTime()) / 1000),
  12.                 day_diff = Math.floor(diff / 86400);
  13.                        
  14.         if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
  15.                 return;
  16.                        
  17.         return day_diff == 0 && (
  18.                         diff < 60 && "just now" ||
  19.                         diff < 120 && "1 minute ago" ||
  20.                         diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  21.                         diff < 7200 && "1 hour ago" ||
  22.                         diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  23.                 day_diff == 1 && "Yesterday" ||
  24.                 day_diff < 7 && day_diff + " days ago" ||
  25.                 day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
  26. }
  27.  
  28. // If jQuery is included in the page, adds a jQuery plugin to handle it as well
  29. if ( typeof jQuery != "undefined" )
  30.         jQuery.fn.prettyDate = function(){
  31.                 return this.each(function(){
  32.                         var date = prettyDate(this.title);
  33.                         if ( date )
  34.                                 jQuery(this).text( date );
  35.                 });
  36.         };