# Formatting an expiring time in days, minutes and seconds

**URL:** <https://www.ruby-forum.com/t/formatting-an-expiring-time-in-days-minutes-and-seconds/88871>\
**Category:** Rails\
**Created:** [March 15, 2007, 5:21am UTC](https://www.ruby-forum.com/t/formatting-an-expiring-time-in-days-minutes-and-seconds/88871 "2007-03-15T05:21:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [March 15, 2007, 5:21am UTC](https://www.ruby-forum.com/t/formatting-an-expiring-time-in-days-minutes-and-seconds/88871/1 "2007-03-15T05:21:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [March 15, 2007, 6:16am UTC](https://www.ruby-forum.com/t/formatting-an-expiring-time-in-days-minutes-and-seconds/88871/2 "2007-03-15T06:16:12Z")

</div>

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…

> **[ActionView::Helpers::DateHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M000574)**
>
> Action View Date Helpers The Date Helper primarily creates select/option tags for different kinds of dates and times or date and time elements.

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
