Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created March 26, 2012 13:06
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save badsyntax/2204919 to your computer and use it in GitHub Desktop.
Save badsyntax/2204919 to your computer and use it in GitHub Desktop.
Javascript date time ago
Ext.Date.fuzzy = function(time, local){
(!local) && (local = Date.now());
if (typeof time !== 'number' || typeof local !== 'number') {
return;
}
var
offset = Math.abs((local - time)/1000),
span = [],
MINUTE = 60,
HOUR = 3600,
DAY = 86400,
WEEK = 604800,
MONTH = 2629744,
YEAR = 31556926;
DECADE = 315569260;
if (offset <= MINUTE) span = [ '', 'moments' ];
else if (offset < (MINUTE * 60)) span = [ Math.round(Math.abs(offset / MINUTE)), 'min' ];
else if (offset < (HOUR * 24)) span = [ Math.round(Math.abs(offset / HOUR)), 'hr' ];
else if (offset < (DAY * 7)) span = [ Math.round(Math.abs(offset / DAY)), 'day' ];
else if (offset < (WEEK * 52)) span = [ Math.round(Math.abs(offset / WEEK)), 'week' ];
else if (offset < (YEAR * 10)) span = [ Math.round(Math.abs(offset / YEAR)), 'year' ];
else if (offset < (DECADE * 100)) span = [ Math.round(Math.abs(offset / DECADE)), 'decade' ];
else span = [ '', 'a long time' ];
span[1] += (span[0] === 0 || span[0] > 1) ? 's' : '';
span = span.join(' ');
return (time <= local) ? span + ' ago' : 'in ' + span;
};
@anupsingh244
Copy link

anupsingh244 commented Aug 22, 2016

abc c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment