Skip to content

Instantly share code, notes, and snippets.

@turtlesoupy
Created January 6, 2012 08: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 turtlesoupy/1569595 to your computer and use it in GitHub Desktop.
Save turtlesoupy/1569595 to your computer and use it in GitHub Desktop.
Simple CoffeeScript distanceOfTimeInWords and distanceOfTimeInWordsToNow
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