Browse Source

Make the humanify dates nicer

master
Andrew Chilton 10 years ago
parent
commit
d222f5dbfb
2 changed files with 46 additions and 17 deletions
  1. +44
    -15
      jquery.humanify.js
  2. +2
    -2
      jquery.humanify.min.js

+ 44
- 15
jquery.humanify.js View File

@@ -46,23 +46,52 @@
// public functions
$.fn.humanify.pretty = function(time) {
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," "));
var diff = (((new Date()).getTime() - date.getTime()) / 1000);
var day_diff = Math.floor(diff / 86400);
var sec_diff = ( ((new Date()).getTime() - date.getTime()) / 1000 );
var day_diff = Math.floor( sec_diff / 86400 );

if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return;
if ( isNaN(day_diff) || day_diff < 0 )
return 'Future'; // ??

return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago"
) ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago"
;
var week_diff = Math.floor(day_diff / 7);
var month_diff = Math.floor(day_diff / 30);
var year_diff = Math.floor(day_diff / 365);

if ( sec_diff < 60 ) {
return 'moments ago';
}
else if ( sec_diff < 120 ) {
return 'a minute ago';
}
else if ( sec_diff < 3600 ) {
return Math.floor( sec_diff / 60 ) + ' minutes ago';
}
else if ( sec_diff < 7200 ) {
return 'an hour ago';
}
else if ( sec_diff < 86400 ) {
return Math.floor( sec_diff / 3600 ) + ' hours ago';
}
else if ( day_diff == 1 ) {
return 'yesterday';
}
else if ( day_diff < 7 ) {
return day_diff + ' days ago';
}
else if ( week_diff == 1 ) {
return 'a week ago';
}
else if ( month_diff == 0 ) {
return week_diff + ' weeks ago';
}
else if ( month_diff == 1 ) {
return 'a month ago';
}
else if ( month_diff < 12 ) {
return month_diff + ' months ago';
}

// return original time
return time;
};




+ 2
- 2
jquery.humanify.min.js View File

@@ -1,2 +1,2 @@
(function(c){c.fn.humanify=function(a){c.extend({},c.fn.humanify.defaults,a);return this.each(function(){var b=c(this),d=b.attr("data-date");b.text(c.fn.humanify.pretty(d.substr(0,19)))})};c.fn.humanify.pretty=function(a){a=new Date((a||"").replace(/-/g,"/").replace(/[TZ]/g," "));a=((new Date).getTime()-a.getTime())/1E3;var b=Math.floor(a/86400);if(!(isNaN(b)||b<0||b>=31))return b==0&&(a<60&&"just now"||a<120&&"1 minute ago"||a<3600&&Math.floor(a/60)+" minutes ago"||a<7200&&"1 hour ago"||a<86400&&
Math.floor(a/3600)+" hours ago")||b==1&&"Yesterday"||b<7&&b+" days ago"||b<31&&Math.ceil(b/7)+" weeks ago"};c.fn.humanify.defaults={}})(jQuery);
(function(c){c.fn.humanify=function(d){c.extend({},c.fn.humanify.defaults,d);return this.each(function(){var a=c(this),b=a.attr("data-date");a.text(c.fn.humanify.pretty(b.substr(0,19)))})};c.fn.humanify.pretty=function(d){var a=new Date((d||"").replace(/-/g,"/").replace(/[TZ]/g," "));a=((new Date).getTime()-a.getTime())/1E3;var b=Math.floor(a/86400);if(isNaN(b)||b<0)return"Future";var f=Math.floor(b/7),e=Math.floor(b/30);if(a<60)return"moments ago";else if(a<120)return"a minute ago";else if(a<3600)return Math.floor(a/
60)+" minutes ago";else if(a<7200)return"an hour ago";else if(a<86400)return Math.floor(a/3600)+" hours ago";else if(b==1)return"yesterday";else if(b<7)return b+" days ago";else if(f==1)return"a week ago";else if(e==0)return f+" weeks ago";else if(e==1)return"a month ago";else if(e<12)return e+" months ago";return d};c.fn.humanify.defaults={}})(jQuery);

Loading…
Cancel
Save