VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app

How To Write Your Own Time Comparison Function in Ruby Like time_ago_in_words Without Rails

       

  # time_ago_in_words(Time.now, 1.day.ago) # => 1 day
  # time_ago_in_words(Time.now, 1.hour.ago) # => 1 hour
  def time_ago_in_words(t1, t2)
    s = t1.to_i - t2.to_i # distance between t1 and t2 in seconds

    resolution = if s > 29030400 # seconds in a year
      [(s/29030400), 'years'] 
    elsif s > 2419200
      [(s/2419200), 'months']
    elsif s > 604800
      [(s/604800), 'weeks']
    elsif s > 86400
      [(s/86400), 'days']
    elsif s > 3600 # seconds in an hour
      [(s/3600), 'hours'] 
    elsif s > 60
      [(s/60), 'minutes']
    else
      [s, 'seconds']
    end

    # singular v. plural resolution
    if resolution[0] == 1
      resolution.join(' ')[0...-1]
    else
      resolution.join(' ')
    end
  end
Tagged w/ #sinatra #ruby #diy #date #helpers