Module: PrettyDate

Defined in:
lib/pretty_date.rb,
lib/pretty_date/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.format(date) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pretty_date.rb', line 4

def format date
  diff = (Time.now.to_i - date.to_i)
  date_diff = (diff / 86400).floor

  if date_diff == 0
    if diff < 60
      'just now'
    elsif diff < 120
      '1 minute ago'
    elsif diff < 3600
      "#{(diff / 60).floor} minutes ago"
    elsif diff < 7200
      '1 hour ago'
    elsif diff < 86400
      "#{(diff / 3600).floor} hours ago"
    end
  elsif date_diff == 1
    'yesterday'
  elsif date_diff < 7
    "#{date_diff} days ago"
  elsif date_diff == 7
    "1 week ago"
  elsif date_diff < 31
    "#{(date_diff / 7).floor} weeks ago"
  end
end