public
Last active

Custom date/time formats in Ruby on Rails

  • Download Gist
datetime.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[Time, Date].map do |klass|
klass::DATE_FORMATS[:variable] = lambda do |t|
date =
case
# display today's date as "today"
when t >= Date.today then "today"
# display yesterday's date as 'Yesterday'
when (t >= Date.yesterday and t <= Date.today) then "yesterday"
# otherwise, display date: eg. Mon 4th June
else
t.strftime("%A #{t.day.ordinalize} %B")
end
time = t.strftime(" #{t.strftime("%I").to_i}:%M%p").downcase
date + time
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.