# Anyone know of a humanised date difference plugin/script

**URL:** <https://www.ruby-forum.com/t/anyone-know-of-a-humanised-date-difference-plugin-script/132496>\
**Category:** Rails\
**Created:** [March 20, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/anyone-know-of-a-humanised-date-difference-plugin-script/132496 "2008-03-20T19:26:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [March 20, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/anyone-know-of-a-humanised-date-difference-plugin-script/132496/1 "2008-03-20T19:26:27Z")

</div>

I’m looking for something that when given two dates would give me  
something like ‘3 days ago’ or ‘2 hours ago’ etc. I’ve done quite a  
bit of searching and either I’m not searching on the right stuff or  
there is nothing out there.

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [March 20, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/anyone-know-of-a-humanised-date-difference-plugin-script/132496/2 "2008-03-20T19:28:48Z")

</div>

> I’m looking for something that when given two dates would give me  
> something like ‘3 days ago’ or ‘2 hours ago’ etc. I’ve done quite a  
> bit of searching and either I’m not searching on the right stuff or  
> there is nothing out there.

From Rails istelf (1.2.5 docs)

distance\_of\_time\_in\_words(from\_time, to\_time = 0, include\_seconds =  
false)

Reports the approximate distance in time between two Time or Date  
objects  
or integers as seconds. Set include\_seconds to true if you want more  
detailed approximations when distance \< 1 min, 29 secs Distances are  
reported base on the following table:

0 \<-\> 29 secs # =\> less than a minute 30 secs \<-\> 1 min, 29 secs # =\> 1  
minute 1 min, 30 secs \<-\> 44 mins, 29 secs # =\> [2…44] minutes 44 mins,  
30 secs \<-\> 89 mins, 29 secs # =\> about 1 hour 89 mins, 29 secs \<-\> 23  
hrs, 59 mins, 29 secs # =\> about [2…24] hours 23 hrs, 59 mins, 29 secs  
\<-\> 47 hrs, 59 mins, 29 secs # =\> 1 day 47 hrs, 59 mins, 29 secs \<-\> 29  
days, 23 hrs, 59 mins, 29 secs # =\> [2…29] days 29 days, 23 hrs, 59  
mins,  
30 secs \<-\> 59 days, 23 hrs, 59 mins, 29 secs # =\> about 1 month 59  
days,  
23 hrs, 59 mins, 30 secs \<-\> 1 yr minus 31 secs # =\> [2…12] months 1 yr  
minus 30 secs \<-\> 2 yrs minus 31 secs # =\> about 1 year 2 yrs minus 30  
secs \<-\> max time or date # =\> over [2…X] years

With include\_seconds = true and the difference \< 1 minute 29 seconds 0-4  
secs # =\> less than 5 seconds 5-9 secs # =\> less than 10 seconds 10-19  
secs # =\> less than 20 seconds 20-39 secs # =\> half a minute 40-59 secs

# 

=\> less than a minute 60-89 secs # =\> 1 minute

Examples:

from\_time = Time.now  
distance\_of\_time\_in\_words(from\_time, from\_time + 50.minutes) # =\>  
about  
1 hour  
distance\_of\_time\_in\_words(from\_time, from\_time + 15.seconds) # =\>  
less  
than a minute  
distance\_of\_time\_in\_words(from\_time, from\_time + 15.seconds, true) #  
=\>  
less than 20 seconds

Note: Rails calculates one year as 365.25 days.

---

<div class="post-metadata">

**Author:** ![DEfusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e68b1a/32.png) [@DEfusion](https://www.ruby-forum.com/u/DEfusion)\
**Post date:** [March 20, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/anyone-know-of-a-humanised-date-difference-plugin-script/132496/3 "2008-03-20T21:50:12Z")

</div>

Sweet, I knew I was sure I’d seen it somewhere before but really  
couldn’t remember and it was one of those times where what I was  
searching for didn’t really describe what I was looking for properly.
