# Feature proposal: Add units option to distance\_of\_time\_in\_words helper

**URL:** <https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646>\
**Category:** rubyonrails-core\
**Tags:** patch, feature\
**Created:** [February 13, 2025, 3:55pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646 "2025-02-13T15:55:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peer\_Allan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/peer_allan/32/13149_2.png) [@Peer\_Allan](https://discuss.rubyonrails.org/u/Peer_Allan)\
**Post date:** [February 13, 2025, 3:55pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646/1 "2025-02-13T15:55:50Z")

</div>

In our app we need to display the amount of time passed since an object was created in the UI. Out of the box it looks like `time_ago_in_words` was going to do the job. However, in our case we need to know the time in a specific unit, in this case days. By happenstance the important numbers are in the 120 days range which causes `time_ago_in_words` to swap to ‘months’. We had to create custom method to handle this which partly duplicates the Rails code.

I have a patch to `distance_of_time_in_words` that allows the user to specify a `units:` option to force the returned value to be in the unit defined. For example,

```ruby
?> distance_of_time_in_words(5.hours, 0, units: :minutes)
=> "300 minutes"
?> distance_of_time_in_words(5.hours, 0, units: :days)
=> "0 days"
?> distance_of_time_in_words(128.days, 0, units: :days)
=> "128 days"
?> distance_of_time_in_words(380.days, 0, units: :months)
=> "13 months"
?> distance_of_time_in_words(128.days, 0, units: :years)
=> "about 0 years"

```

This would uses the locale code that is already present in the method. Right now I didn’t consider a fallback if a time gets large, (e.g. user chooses minutes but the diff is 5 years so it results in a very large minute value) as it the user’s choice to keep that.

Looking for feedback and/or opinions on pushing such a patch up.

---

<div class="post-metadata">

**Author:** ![djmolny](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/djmolny/32/11811_2.png) [@djmolny](https://discuss.rubyonrails.org/u/djmolny)\
**Post date:** [February 13, 2025, 9:02pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646/2 "2025-02-13T21:02:45Z")

</div>

Huh. I forgot that this helper existed until reading your post.

I agree that its current features are inflexible and not suitable for a wide range of potential use cases. It also seems to violate the “opinion-free” nature of most Rails features, specifically the hard-coded ranges that control when it returns actual values vs. approximations.

But I don’t think extending `distance_of_time_in_words` is the answer. A look at the [source](https://github.com/rails/rails/blob/cf6ff17e9a3c6c1139040b519a341f55f0be16cf/actionview/lib/action_view/helpers/date_helper.rb#L95) reveals that it’s mostly hard-coded values and arbitrary decision points. So despite the method name, it’s not a generic converter of time differences into words.

Why not use simple math to get the number of days? Or create a helper method that does so? `(SomeObj.created_at - Time.now) / SECONDS_PER_DAY`

---

<div class="post-metadata">

**Author:** ![Peer\_Allan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/peer_allan/32/13149_2.png) [@Peer\_Allan](https://discuss.rubyonrails.org/u/Peer_Allan)\
**Post date:** [February 21, 2025, 5:19pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646/3 "2025-02-21T17:19:08Z")

</div>

@djmolny are you saying just create new, separate helpers rather than hacking into the existing method?

For example, `time_ago_in_days` or `time_ago_in_minutes`

---

<div class="post-metadata">

**Author:** ![djmolny](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/djmolny/32/11811_2.png) [@djmolny](https://discuss.rubyonrails.org/u/djmolny)\
**Post date:** [February 21, 2025, 5:38pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646/4 "2025-02-21T17:38:24Z")

</div>

Yes. That should only require a few lines of code.

---

<div class="post-metadata">

**Author:** ![ahmad54](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/ahmad54/32/13431_2.png) [@ahmad54](https://discuss.rubyonrails.org/u/ahmad54)\
**Post date:** [October 24, 2026, 6:12pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-units-option-to-distance-of-time-in-words-helper/88646/5 "2026-10-24T18:12:28Z")

</div>

> [@Peer\_Allan](#):
>
> In our app we need to display the amount of time passed since an object was created in the UI. Out of the box it looks like `time_ago_in_words` was going to do the job. However, in our case we need to know the time in a specific unit, in this case days. By happenstance the important [calculate numbers](https://laacalculadoraalicia.es/) are in the 120 days range which causes `time_ago_in_words` to swap to ‘months’. We had to create custom method to handle this which partly duplicates the Rails code.
> 
> I have a patch to `distance_of_time_in_words` that allows the user to specify a `units:` option to force the returned value to be in the unit defined. For example,
> 
> ```ruby
> ?> distance_of_time_in_words(5.hours, 0, units: :minutes)
> => "300 minutes"
> ?> distance_of_time_in_words(5.hours, 0, units: :days)
> => "0 days"
> ?> distance_of_time_in_words(128.days, 0, units: :days)
> => "128 days"
> ?> distance_of_time_in_words(380.days, 0, units: :months)
> => "13 months"
> ?> distance_of_time_in_words(128.days, 0, units: :years)
> => "about 0 years"
> 
> ```
> 
> This would uses the locale code that is already present in the method. Right now I didn’t consider a fallback if a time gets large, (e.g. user chooses minutes but the diff is 5 years so it results in a very large minute value) as it the user’s choice to keep that.
> 
> Looking for feedback and/or opinions on pushing such a patch up.

I think this would be a useful addition. Being able to specify the unit gives developers more control without having to duplicate the existing Rails code.
