Created
October 21, 2013 10:42
-
-
Save oswaldoacauan/7081881 to your computer and use it in GitHub Desktop.
Javascript - Time Since function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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