Skip to content

Instantly share code, notes, and snippets.

@jcf
Created July 4, 2011 16:28
Show Gist options
  • Save jcf/1063575 to your computer and use it in GitHub Desktop.
Save jcf/1063575 to your computer and use it in GitHub Desktop.
Convert a Twitter created_at timestamp to something more human friendly
# Based upon John Resig's Pretty Date
# http://ejohn.org/blog/javascript-pretty-date/
Helpers = ->
@convert_twitter_timestamp = (time) ->
date = new Date(Date.parse(time))
diff = (((new Date()).getTime() - date.getTime()) / 1000)
day_diff = Math.floor(diff / 86400)
if diff < 60
"just now"
else if diff < 120
"1 minute ago"
else if diff < 3600
"#{Math.floor(diff / 60)} minutes ago"
else if diff < 7200
"1 hour ago"
else if diff < 86400
"#{Math.floor(diff / 3600)} hours ago"
else if day_diff == 1
"Yesterday"
else if day_diff < 7
"#{day_diff} days ago"
else if day_diff == 7
"1 week ago"
else if day_diff < 31
"#{Math.ceil(day_diff / 7)} weeks ago"
else
"A long time ago"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment