在Rails 3或Ruby中将持续时间转换为小时:分钟:秒(或类似) [英] Convert duration to hours:minutes:seconds (or similar) in Rails 3 or Ruby

查看:96
本文介绍了在Rails 3或Ruby中将持续时间转换为小时:分钟:秒(或类似)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感觉有一种简单/内置的方法可以执行此操作,但我找不到它。

I have a feeling there is a simple/built-in way to do this but I can't find it.

我有一个持续时间(以秒为单位)

I have a duration (in seconds) in an integer and I want to display it in a friendly format.

例如,以整数形式显示。 3600会显示为 01:00:00或 1 hour之类。

e.g. 3600 would be displayed as "01:00:00" or "1 hour" or something.

我可以使用 time_ago_in_words(Time。 zone.now + 3600),但感觉有点像破解,没有理由从当前时间开始添加/减去该格式的值。是否有 duration_in_words()或其他东西?

I can do it with time_ago_in_words(Time.zone.now+3600) but that feels like a bit of a hack, there is no reason to add/subtract from the current time just to format this value. Is there a duration_in_words() or something?

谢谢

推荐答案

请参阅: http:// api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html

distance_of_time_in_words(3600)
 => "about 1 hour"

这篇关于在Rails 3或Ruby中将持续时间转换为小时:分钟:秒(或类似)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆