views:

38

answers:

1

In the mailbox section of my Rails app, I simply display the timestamps of each message as follows:

<%= message.created_at.utc %>

But I want to change it so that if the message's date is different to today, then display the date, otherwise just display the time (e.g. like Gmail).

+1  A: 

Try putting something like this in a helper:

def time_or_date(date)
  date.today? ? date.strftime('%H:%M') : date.strftime('%d/%m')
end

You can then call it from your view like this:

<%= time_or_date(message.created_at.utc) %>

You might want to change strftime so it fits your needs: http://www.ruby-doc.org/core/classes/Time.html#M000297

magnushjelm