I have a number of seconds. Let's say 270921. How can I display that number saying it is xx days, yy hours, zz minutes, ww seconds?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- How to account for clock offsets in a distributed
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
- ruby - simplify string multiply concatenation
I was hoping there would be an easier way than using divmod, but this is the most DRY and reusable way I found to do it:
The method is easily adjusted by changing the format string and the first inline array (ie the [24,60,60]).
Enhanced version
Examples:
Note that
format_string
won't let you change the order of the parts (it's always the most significant value to least). For finer grained control, you can usesplit
and manipulate the values yourself.If you're using Rails, there is an easy way if you don't need the precision: