Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <%= time_ago_in_words(timestamp) %>
  2.  
  3. module PrettyDate
  4. def to_pretty
  5. a = (Time.now-self).to_i
  6.  
  7. case a
  8. when 0 then 'just now'
  9. when 1 then 'a second ago'
  10. when 2..59 then a.to_s+' seconds ago'
  11. when 60..119 then 'a minute ago' #120 = 2 minutes
  12. when 120..3540 then (a/60).to_i.to_s+' minutes ago'
  13. when 3541..7100 then 'an hour ago' # 3600 = 1 hour
  14. when 7101..82800 then ((a+99)/3600).to_i.to_s+' hours ago'
  15. when 82801..172000 then 'a day ago' # 86400 = 1 day
  16. when 172001..518400 then ((a+800)/(60*60*24)).to_i.to_s+' days ago'
  17. when 518400..1036800 then 'a week ago'
  18. else ((a+180000)/(60*60*24*7)).to_i.to_s+' weeks ago'
  19. end
  20. end
  21. end
  22.  
  23. Time.send :include, PrettyDate
  24.  
  25. 30.seconds.ago
  26. 2.days.ago
  27.  
  28. <%= time_ago_in_words(Date.today - 1) %>
  29.  
  30. include ActionView::Helpers::DateHelper
  31. def index
  32. @sexy_date = time_ago_in_words(Date.today - 1)
  33. end
  34.  
  35. Time.now - 2.days
  36.  
  37. def relative_time(start_time)
  38. diff_seconds = Time.now - start_time
  39. case diff_seconds
  40. when 0 .. 59
  41. puts "#{diff_seconds} seconds ago"
  42. when 60 .. (3600-1)
  43. puts "#{diff_seconds/60} minutes ago"
  44. when 3600 .. (3600*24-1)
  45. puts "#{diff_seconds/3600} hours ago"
  46. when (3600*24) .. (3600*24*30)
  47. puts "#{diff_seconds/(3600*24)} days ago"
  48. else
  49. puts start_time.strftime("%m/%d/%Y")
  50. end
  51. end
  52.  
  53. Time.now.yesterday
  54. Time.now.ago(2.days).end_of_day
  55. Time.now.next_month.beginning_of_month
  56.  
  57. <%= times_ago_in_words(comment.created_at) %>
  58.  
  59. <abbr class="timeago" title="<%= comment.created_at.getutc.iso8601 %>">
  60. <%= comment.created_at.to_s %>
  61. </abbr>
  62.  
  63. $("abbr.timeago").timeago();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement