distance_of_time_in_words但只是(HR:MIN:SEC)?

时间:2011-11-13 15:20:30

标签: ruby-on-rails ruby datetime

我正在寻找类似于distance_of_time_in_words()的东西,而不是'in_words'部分我希望采用与此类似的格式:

'小时:分钟:秒'

甚至:

'天:小时:分钟:秒'

有一种简单的自定义方式吗?

编辑:找到相关帖子,回答: Rails: distance_of_time_NOT_in_words

1 个答案:

答案 0 :(得分: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)