How do I form "fuzzy" date/time from RFC 2822 formatted (Sat, 18 Jul 2009 10:57:43 +0300) timestamp?
With fuzzy date I mean like: "5 minutes ago", "2 days, 15 minutes ago".
How do I form "fuzzy" date/time from RFC 2822 formatted (Sat, 18 Jul 2009 10:57:43 +0300) timestamp?
With fuzzy date I mean like: "5 minutes ago", "2 days, 15 minutes ago".
Rails gives your views a helper function called time_ago_in_words
that you can call to output just such a format from a Time
object.
def fuzzy_date(date)
date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
days = (date - Date.today).to_i
return 'today' if days >= 0 and days < 1
return 'tomorrow' if days >= 1 and days < 2
return 'yesterday' if days >= -1 and days < 0
return "in #{days} days" if days.abs < 60 and days > 0
return "#{days.abs} days ago" if days.abs < 60 and days < 0
return date.strftime('%A, %B %e') if days.abs < 182
return date.strftime('%A, %B %e, %Y')
end
There's some really powerful date/time parsers in ruby (unfortunately kind of hard to google(TM) for)