Created
April 14, 2016 15:45
-
-
Save theavish/2424171d0165d6b059596b3da24db365 to your computer and use it in GitHub Desktop.
time since ____
This file contains hidden or 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)) | |
| var interval = Math.floor(seconds / 31536000); | |
| if (interval >= 1) { | |
| if(interval == 1) return interval + " year ago"; | |
| else | |
| return interval + " years ago"; | |
| } | |
| interval = Math.floor(seconds / 2592000); | |
| if (interval >= 1) { | |
| if(interval == 1) return interval + " month ago"; | |
| else | |
| return interval + " months ago"; | |
| } | |
| interval = Math.floor(seconds / 86400); | |
| if (interval >= 1) { | |
| if(interval == 1) return interval + " day ago"; | |
| else | |
| return interval + " days ago"; | |
| } | |
| interval = Math.floor(seconds / 3600); | |
| if (interval >= 1) { | |
| if(interval == 1) return interval + " hour ago"; | |
| else | |
| return interval + " hours ago"; | |
| } | |
| interval = Math.floor(seconds / 60); | |
| if (interval >= 1) { | |
| if(interval == 1) return interval + " minute ago"; | |
| else | |
| return interval + " minutes ago"; | |
| } | |
| return Math.floor(seconds) + " seconds ago"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment