Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * JavaScript Pretty Date
  3.  * Copyright (c) 2008 John Resig (jquery.com)
  4.  * Licensed under the MIT license.
  5.  * Modified 2011 Charles Smith (phase2technology.com)
  6.  */
  7.  
  8. // Takes an ISO time and returns a string representing how
  9. // long ago the date represents.
  10. function prettyDate(time){
  11.   var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
  12.     diff = (((new Date()).getTime() - date.getTime()) / 1000),
  13.     day_diff = Math.floor(diff / 86400);
  14.  
  15.   if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
  16.     return;
  17.  
  18.   var monthNames = [ "January", "February", "March", "April", "May", "June",
  19.                      "July", "August", "September", "October", "November", "December" ];
  20.  
  21.   return day_diff == 0 && (
  22.       diff < 60 && "just now" ||
  23.       diff < 120 && "1 minute ago" ||
  24.       diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  25.       diff < 7200 && "1 hour ago" ||
  26.       diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  27.     day_diff == 1 && "Yesterday" ||
  28.     day_diff > 1 && monthNames[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
  29.  
  30. }
  31.  
  32. // If jQuery is included in the page, adds a jQuery plugin to handle it as well
  33. if ( typeof jQuery != "undefined" )
  34.   jQuery.fn.prettyDate = function(){
  35.     return this.each(function(){
  36.       var date = prettyDate(this.title);
  37.       if ( date )
  38.         jQuery(this).text( date );
  39.     });
  40.   };
  41.  
  42. (function($){
  43.   $(document).ready(function(){
  44.     $('.prettydate').prettyDate();
  45.     setInterval(function(){ $('.prettydate').prettyDate(); }, 5000);
  46.   });
  47. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement