Skip to content

Instantly share code, notes, and snippets.

@mm53bar
Created May 12, 2009 15:33
Show Gist options
  • Save mm53bar/110540 to your computer and use it in GitHub Desktop.
Save mm53bar/110540 to your computer and use it in GitHub Desktop.
# from http://rpheath.com/posts/409-rails-time-date-formatting-with-lambda
# config/initializers/date_time_formats.rb
Time::DATE_FORMATS.merge!(
:full => '%B %d, %Y at %I:%M %p',
:md => '%m/%d',
:mdy => '%m/%d/%y',
:time => '%I:%M %p'
)
# Allows you to do Time.now.to_s(:full)
#
# Can also add logic using lambdas
Time::DATE_FORMATS.merge!(
:friendly => lambda { |time|
if time.year == Time.now.year
time.strftime "%b #{time.day.ordinalize}"
else
time.strftime "%b #{time.day.ordinalize}, %Y"
end
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment