Created
January 6, 2012 08:00
-
-
Save turtlesoupy/1569595 to your computer and use it in GitHub Desktop.
Simple CoffeeScript distanceOfTimeInWords and distanceOfTimeInWordsToNow
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
distanceOfTimeInWords = (dateDiff) -> | |
seconds = Math.abs(dateDiff) / 1000 | |
minutes = seconds / 60 | |
hours = minutes / 60 | |
days = hours / 24 | |
years = days / 365 | |
words = switch | |
when seconds < 45 then "less than a minute" | |
when seconds < 90 then "about a minute" | |
when minutes < 45 then "#{Math.round minutes} minutes" | |
when minutes < 90 then "about an hour" | |
when hours < 24 then "about #{Math.round hours} hours" | |
when hours < 48 then "a day" | |
when days < 30 then "#{Math.floor days} days" | |
when days < 60 then "about a month" | |
when days < 365 then "#{Math.floor (days/30)} months" | |
when years < 2 then "about a year" | |
else "#{Math.floor years} years" | |
if dateDiff < 0 then "#{words} ago" else "#{words} from now | |
distanceOfTimeInWordsToNow = (date) -> distanceOfTimeInWords(date - new Date()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment