Humanizing dates


What would be the easiest way to pick up a Date or DateTime object and
“humanize” them, like:

  • Next wednesday
  • Last friday
  • 10 hours ago
  • Next month

Is there a Ruby library, gem or Rails plugin that helps on assembling
phrases from date/datetime information?

Could someone put me on the right direciton?



be like what you’re looking for (from

James Brooks
Good Dog Design

On Thu, Aug 7, 2008 at 9:54 AM, Marcelo de Moraes S.
<[email protected]

Thanks a lot!

This solves almost all of my specs.

One thing that I’m trying to figure out,though, is, given a date, output
something like:

“Last wednesday” or “Next friday”…

Would you know a solution from the top of your head?


I don’t know of something that works that way around, that you might
have to
code yourself. Perhaps you could get inspiration from a gem called
(which does the reverse of what you want, e.g. It can take a string
wednesday” and calculate a date)… Maybe it can do the reverse, I’ve
checked :slight_smile:

James Brooks
Good Dog Design

On Thu, Aug 7, 2008 at 11:08 AM, Marcelo de Moraes S. <

I saw this and hacked up some code:

You should be able to put that in your lib/ folder in your app, do
require ‘proximity’ in your config/environment.rb file and then you
can call date.to_words. The same code could be applied to Time as well

Thanks James, I will do that.

Clever and simple solution, perfect.

The issue with date/time is that things can get ambiguous and complex.
solution provided before could work to some extent and were similar but
more complex and while I could add code to support the use-case, I’m
just so
glad you did and shared it! Thanks a lot!


Hey Philip,

I think that, for weekday names, you have to keep you have to take into
account the week (7 days).

For example, let’s say we have a Date object that “A” represents
and another one “B” that represents 18/08/2008. Both are mondays.

Now, we have a Date object that represents today - 07/08/2008.

If you load Ryan’s code and do this:


The following will happen:

  1. Is this date the same as today ( If so, output “Today”;
  2. Is this date the same as yesterday ( If so, output
  3. Is this date included in the set = {today - 7 (one week ago) …
    yesterday} - this will essentially create an array that has date objects
    from one week ago until yesterday. If the date is included in this
    and as we have considered it to be “last week” and as we know that the
    is not yesterday, nor the current date nor tomorrow, we can say this
    was the last “weekday”.
  4. Applies the same logic as above, but inverted (one week from now).

Now, things get a little more complicated. For example, the next test

elsif (( + 8)…( + 14)).include?(self)
“Two #{self.strftime(”%A")}s away"

What does that mean? From what I could understand, the test checks if
Date object being converted is in a period of time that starts one week

  • 1
    from now ( ss ) and ends two weeks - 1 from now. Considering we have the
    following date object: 14/08/2008 and today is today’s date

today + 8 = 15/08/2008
today + 14 = 21/08/2008

S M Tu W Th F S S M Tu W Th { F S S M Tu W Th } F S S …
| |
Today 14/08/2008 |


This test wouldn’t catch our date object, but the previous one would do
(Next #{weekday}).

But one important point is that he seems to build upon last the previous
tests, so I think the order is important, and also explains the odd
he does in the checks, the code “considers” something like “hey, if this
date is not tomorrow then I can omit tomorrow from the next check…”.

Calculations with Dates are confusing, at least to me. That’s how I
interpret Ryan’s code, and I’m sure it is not a complete view yet. I
know if there’s a pattern for such thing or any way to simplify this
that’s with Ryan!

Please, correct me if I’m wrong!


I tried simplifying the code this morning, first it would check to see
if the date was today, if so just print today, then it would check if
the date is less than today and if so run all the checks for dates
less than today, else run all the checks for dates greater than today.
I think I updated the gist (can’t check, on train writing emails
offline) and there’s a person called lachie who forked it which may
make the code a bit cleaner for you.

That is neat. I don’t have anything real to contribute other than for
some odd reason I was talking about this issue last night with my wife
(not ruby, but the date thing).

Let’s say it’s Thursday.

Is “next saturday” in two days? Or in nine? We decided it was nine.
“this saturday” is in two days. But we agreed that “the next
saturday” is in two days.

So, when you say “next saturday” which do you mean? :slight_smile:

Maybe it’s just us and the folks around us that gets confused by that,
but it’s something to consider.


Cool. Just checked it out. I’m glad my post turned out into something
for the community :slight_smile:

Also, I would like to note that this could be used together with the
distance_in_time_in_words. Even though they appear to be similar, they
in different use cases. I can see that distance_in_time_in_words tries
do be
more general and count in days/months and years, not weeks, also it
print weekdays. But it is useful for building more granular historical
like “less than 1 hour ago”, and Ryan’s to_word more useful for schedule
calendar use-cases.



I just read Philip’s post and I realised that +
1… + 7 says "Next ", instead of "This …
interesting point. I’ve updated the gist again:

It will now display “this coming Friday” for example of it’s within
the range of 6 days from today. This should mean that it’ll say “this
coming Thursday” for thursday next week, and “next Friday” for the
friday that follows.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs