You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

40 lines
1.3 KiB

  1. /*
  2. * JavaScript Pretty Date
  3. * Copyright (c) 2011 John Resig (ejohn.org)
  4. * Licensed under the MIT and GPL licenses.
  5. */
  6. // Takes an ISO time and returns a string representing how
  7. // long ago the date represents.
  8. function prettyDate(time){
  9. if(!time) return ''
  10. var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, "")),
  11. diff = (((new Date()).getTime() - date.getTime()) / 1000),
  12. day_diff = Math.floor(diff / 86400);
  13. if ( isNaN(day_diff) || day_diff < 0 )
  14. return '';
  15. return day_diff == 0 && (
  16. diff < 60 && "just now" ||
  17. diff < 120 && "1 minute ago" ||
  18. diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  19. diff < 7200 && "1 hour ago" ||
  20. diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  21. day_diff == 1 && "Yesterday" ||
  22. day_diff < 7 && day_diff + " days ago" ||
  23. day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
  24. day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
  25. "> " + Math.floor( day_diff / 365 ) + " year(s) ago";
  26. }
  27. // If jQuery is included in the page, adds a jQuery plugin to handle it as well
  28. if ( typeof jQuery != "undefined" )
  29. jQuery.fn.prettyDate = function(){
  30. return this.each(function(){
  31. var date = prettyDate(this.title);
  32. if ( date )
  33. jQuery(this).text( date );
  34. });
  35. };