Formatting an expiring time in days, minutes and seconds

Hi,

I have an expiry date. I want to format it in such a way that it will
read “Expires in x days, x hours and x minutes”. Is there an easy way
to do this with some of the date/time formatting of ruby?

Thanks,

Scott

Rails does have some built in methods to format time for humans. You
might be interested in distance_of_time_in_words_to_now (or the
shorter named, identical, less intuitive, time_ago_in_words). I’ve
used them before with fine results, but you should read the API
carefully…

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M000574

It doesn’t provide the level of detail you asked for in your post…
it rounds up to “12 minutes ago” or once more than a month has passed,
it goes to “about a month ago”, “about 3 months ago” etc. It WILL get
specific below 1 minute 29 seconds, but even for that you have to set
an option.

This was good enough for my needs, but might not be good enough for
yours. I haven’t found an elegant way to get exact times.

Good Luck and let me know if you do find something.

—A

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs