How to form a “fuzzy date” in Ruby?

How do I create a “fuzzy” date / time from RFC 2822 in the format (Sat, 07/18/2009 10:57:43 +0300) timestamp?

With a fuzzy date, I mean: "5 minutes ago," "2 days, 15 minutes ago."

+3
source share
3 answers

Rails gives your views a helper function time_ago_in_wordsthat you can call to output such a format from an object Time.

+8
source
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
+3
source

There are some really strong date / time partisans in the ruby ​​(unfortunately, it's hard for Google to use)

http://chronic.rubyforge.org/

http://www.ahabman.com/blog/2009/06/ruby-duration/

http://github.com/flogic/timely/tree/master

+2
source

Source: https://habr.com/ru/post/1712923/


All Articles