Skip to content

Instantly share code, notes, and snippets.

@oswaldoacauan
Created October 21, 2013 10:42
Show Gist options
  • Save oswaldoacauan/7081881 to your computer and use it in GitHub Desktop.
Save oswaldoacauan/7081881 to your computer and use it in GitHub Desktop.
Javascript - Time Since function
function timeSince(date) {
var seconds = Math.floor(((new Date().getTime()/1000) - date)),
interval = Math.floor(seconds / 31536000);
if (interval > 1) return interval + "y";
interval = Math.floor(seconds / 2592000);
if (interval > 1) return interval + "m";
interval = Math.floor(seconds / 86400);
if (interval >= 1) return interval + "d";
interval = Math.floor(seconds / 3600);
if (interval >= 1) return interval + "h";
interval = Math.floor(seconds / 60);
if (interval > 1) return interval + " m";
return Math.floor(seconds) + "s";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment