Bobby Meyer
developer/designer
rails | ruby
Terse Distance of Time in Words
July 20, 2023Rails has a helpful method called distance_of_time_in_words, which displays how long ago a date was in a way that is easy to understand for humans.
By default, the method generates quite lengthy descriptions, like “about 3 years” for a date that is 3 years away. This was causing issues for me when using the method in a chat app to show timestamps on messages, as it was too wordy to display alongside each message.
The good news is that you can customize the default output by redefining it in your locale YAML.
Now a message sent 33 minutes ago will say “~30m” instead of “about 30 minutes” ago.
back to posts