2011-11-13 6 views
1

私は(distance_of_time_in_wordsに似たものを探しています)ではなく、むしろ「in_words」の部分よりも、私はこのような形式で何かをしたいと思います:distance_of_time_in_wordsだけですが(HR:MIN:SEC)?

「時間:分:秒」

かさえ:

「日:時間:分:秒」

これを行う簡単なカスタム方法はありますか?

編集:関連の記事を見つけ、答え: Rails: distance_of_time_NOT_in_words

+1

[Railsの:distance_of_time_NOT_in_words]の可能複製(http://stackoverflow.com/questions/2430249/rails-distance-of-time-not-in-words) – Ryan

答えて

1

はこのここで見つける:http://www.techotopia.com/index.php/Working_with_Dates_and_Times_in_Ruby

は、あなたが探しているものであるように思われます。

require 'date' 

today = DateTime.now 
=> #<DateTime: 441799066630193/180000000,-301/1440,2299161> 

birthday = Date.new(2008, 4, 10) 
=> #<Date: 4909133/2,0,2299161> 

days_to_go = birthday - today 

time_until = birthday - today 
=> Rational(22903369807, 180000000) 

time_until.to_i    # get the number of days until my birthday 
=> 127 

hours,minutes,seconds,frac = Date.day_fraction_to_time(time_until) 
[3053, 46, 57, Rational(1057, 180000000)] 

puts "It is my birthday in #{hours} hours, #{minutes} minutes and #{seconds} seconds (not that I am counting)" 
It is my birthday in 3053 hours, 46 minutes and 57 seconds (not that I am counting) 
+0

完璧、おかげで – easyjo