Skip to content

Instantly share code, notes, and snippets.

@bxh-io
Created September 26, 2013 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxh-io/6713162 to your computer and use it in GitHub Desktop.
Save bxh-io/6713162 to your computer and use it in GitHub Desktop.
App.utility.prettyTime = function(){
//The *1000 here is because python UTC is different in seconds compared to JS UTC in milliseconds
//The this here is normally the function that calls this, but watch out what calls this function
function plural_time(n, period) {
if (n < 2) {
return "1 " + period + " ago";
}
else {
return n + " " + period + "s ago";
}
}
diff = (((new Date()).getTime() - this.get('created')*1000) / 1000);
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 365 )
return 'some time ago';
return day_diff === 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && plural_time(Math.floor( diff / 60 ),"minute") ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && plural_time(Math.floor( diff / 3600 ),"hour")) ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && plural_time(day_diff,"day") ||
day_diff < 31 && plural_time(Math.ceil( day_diff / 7 ),"week") ||
day_diff < 365 && plural_time(Math.ceil( day_diff / 31 ),"month");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment