0

With Rails, how do I write the time out, like this:

2 days 12 hours 5 minutes and 14 seconds

I feel like there's a Date helper that does it, but digging around the docs hasn't surfaced anything.

Shpigford
  • 24,748
  • 58
  • 163
  • 252
  • duplicate http://stackoverflow.com/questions/1679266/can-ruby-print-out-time-difference-duration-readily – mikdiet Apr 25 '12 at 11:46
  • That's called a timespan. [Check this out.](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) – DOK Apr 25 '12 at 11:46
  • And http://stackoverflow.com/questions/4274549/how-can-i-return-a-human-readable-time-range and http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails/4136485#4136485 and... – Dave Newton Apr 25 '12 at 12:21

3 Answers3

1

humantime is one option, but Rails already has time_ago_in_words and distance_of_time_in_words.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

You can use DateTime.strftime. It works the same as the C strftime, so you can find documentation on the web.

DateTime.strftime("%j days %H hours %M minutes and %S seconds")
Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
0

It looks like http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

If not you can be inspired by the source code of this helper

sailor
  • 7,834
  • 3
  • 26
  • 34