Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, display time between two dates in English

In a Rails project I want to find the difference between two dates and then display it in natural language. Something like

>> (date1 - date2).to_natural_language 
"3 years, 2 months, 1 week, 6 days"

Basically this for ruby.

Google and the Rails API haven't turned up anything. I've found some things that will give you the difference in one unit (ie, how many weeks between two dates) but not something that will accurately calculate years, months, weeks, days all together.

like image 721
Eric Wright Avatar asked Jun 30 '09 18:06

Eric Wright


1 Answers

The Rails' ActionView module includes two methods that may do what you require:

  • distance_of_time_in_words
  • distance_of_time_in_words_to_now
like image 111
John Topley Avatar answered Oct 29 '22 10:10

John Topley