Rails 4 - 时间差(Rails 4 - Difference in time)

我有一个开始时间和结束时间,我想计算这两次之间的时间差的持续时间。

我试过使用以下内容

@event.start_time = '13:15' @event.end_time = '16:00' @event.duration = ((@event.end_time - @event.start_time)/1.hour)

持续2 hours 45 min显示2.75 value 。

我想把它显示为2:45。 任何想法我怎么能表明?

I have a start time and end time and i want to calculate the duration of the time difference between those two times.

I have tried to use the following

@event.start_time = '13:15' @event.end_time = '16:00' @event.duration = ((@event.end_time - @event.start_time)/1.hour)

for 2 hours 45 min it shows 2.75 value.

I want to show it as 2:45. any idea how i can show that?

最满意答案

duration_in_sec = (@event.end_time - @event.start_time).to_i @event.duration = "#{duration_in_sec / 3600} hours #{(duration_in_sec % 3600 ) / 60 } min" duration_in_sec = (@event.end_time - @event.start_time).to_i @event.duration = "#{duration_in_sec / 3600} hours #{(duration_in_sec % 3600 ) / 60 } min"

更多推荐