次のように、最も近い一致に簡単に変換したい値のリストが分単位であります
10 => 10 minutes
1440 => 1 day
86400 => 2 months
525600 => 1 year
Railsでこれを行う簡単な方法はありますか?
次のように、最も近い一致に簡単に変換したい値のリストが分単位であります
10 => 10 minutes
1440 => 1 day
86400 => 2 months
525600 => 1 year
Railsでこれを行う簡単な方法はありますか?
試してみてくださいdistance_of_time_in_words
。2 つのオブジェクトを直接操作しTime
て差を計算しますが、いつでも実行できます。
include ActionView::Helpers::DateHelper
def minutes_in_words(minutes)
distance_of_time_in_words(Time.at(0), Time.at(minutes * 60))
end
minutes_in_words(10)
=> "10 minutes"
minutes_in_words(1440)
=> "1 day"
minutes_in_words(86400)
=> "2 months"
minutes_in_words(525600)
=> "about 1 year"
私はおそらくtime_ago_in_words
、操作する適切な日付を取得するために少し計算を使用するでしょう...
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_ago_in_words
time_ago_in_words(3.minutes.from_now) # => 3 minutes
time_ago_in_words(Time.now - 15.hours) # => about 15 hours
time_ago_in_words(Time.now) # => less than a minute
(意図的に)正確ではありませんが、すでに存在しています。それ以外の場合は、除算/モジュロ計算を使用して独自のものを作成するのは非常に簡単です...
RoRにそれがあるとは思わないが、それは簡単だ
def to_days(minutes)
minutes / (60*24)
end
def to_months(minutes)
minutes / (60*24*30)
end
def to_years(minutes)
minutes / (60*24*365)
end
以下のリンクを見てください。必要なものはすべて揃っていると思います。関数 distance_of_time_in_words を見てください。
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html