instance method relative_time_in_words

Ruby on Rails 8.1.2

Since v8.1.2

Signature

relative_time_in_words(from_time, options = {})

Like time_ago_in_words, but adds a prefix/suffix depending on whether the time is in the past or future. You can use the scope option to customize the translation scope. All other options are forwarded to time_ago_in_words.

relative_time_in_words(3.minutes.from_now) # => "in 3 minutes"
relative_time_in_words(3.minutes.ago) # => "3 minutes ago"
relative_time_in_words(10.seconds.ago, include_seconds: true) # => "less than 10 seconds ago"

See also #time_ago_in_words

Parameters

from_time req
options opt = {}
Source
# File actionview/lib/action_view/helpers/date_helper.rb, line 198
      def relative_time_in_words(from_time, options = {})
        now = Time.now
        time = distance_of_time_in_words(from_time, now, options.except(:scope))
        key = from_time > now ? :future : :past

        I18n.t(key, time: time, scope: options.fetch(:scope, :'datetime.relative'), locale: options[:locale])
      end

Defined in actionview/lib/action_view/helpers/date_helper.rb line 198 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::DateHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close