Ruby on rails 用文字表示的时间距离,但仅为(小时:分钟:秒)?

Ruby on rails 用文字表示的时间距离,但仅为(小时:分钟:秒)?,ruby-on-rails,ruby,datetime,Ruby On Rails,Ruby,Datetime,我正在寻找类似于单词中的时间距离的东西,但不是“单词中”部分,我想要类似于以下格式的东西: '小时:分钟:秒' 甚至: '天:小时:分钟:秒' 有没有一个简单的定制方法来做到这一点 编辑:找到相关帖子,回答: 在这里找到: 似乎就是你要找的 require 'date' today = DateTime.now => #<DateTime: 441799066630193/180000000,-301/1440,2299161> birthday = Date.new(20

我正在寻找类似于单词中的时间距离的东西,但不是“单词中”部分,我想要类似于以下格式的东西:

'小时:分钟:秒'

甚至:

'天:小时:分钟:秒'

有没有一个简单的定制方法来做到这一点

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

在这里找到:

似乎就是你要找的

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)
需要“日期”
今天=DateTime.now
=> #
生日=日期。新(2008年4月10日)
=> #
天到日=生日-今天
时间=生日-今天
=>Rational(22903369807180000000)
我要知道离我生日还有多少天
=> 127
小时,分钟,秒,分数=日期。日分数到时间(时间到)
[3053,46,57,Rational(1057180000000)]
写上“今天是我的生日,以{hours}小时、{minutes}分钟和{seconds}秒为单位(我不算在内)”
再过3053小时46分57秒就是我的生日了(我不算在内)
在这里找到:

似乎就是你要找的

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)
需要“日期”
今天=DateTime.now
=> #
生日=日期。新(2008年4月10日)
=> #
天到日=生日-今天
时间=生日-今天
=>Rational(22903369807180000000)
我要知道离我生日还有多少天
=> 127
小时,分钟,秒,分数=日期。日分数到时间(时间到)
[3053,46,57,Rational(1057180000000)]
写上“今天是我的生日,以{hours}小时、{minutes}分钟和{seconds}秒为单位(我不算在内)”
再过3053小时46分57秒就是我的生日了(我不算在内)