2011-11-13 64 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

的可能重複的[滑軌: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

相關問題