Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Formatting a date with or without the time (See related posts)

Ruby on Rails

This helper returns the date (and possibly the time) formatted the way you want

def format_date(date, use_time = false)
    if use_time == true
        ampm = date.strftime("%p").downcase
        new_date = date.strftime("%B %d, %Y at %I:%M" + ampm)
    else
        new_date = date.strftime("%B %d, %Y")
    end
end

Comments on this post

eulogist posts on Dec 21, 2006 at 09:42
...or alternatively, in your environment.rb:

Date::DATE_FORMATS[:foomat] = '%d/%m/%Y'
Time::DATE_FORMATS[:foomat] = '%A %B %d %Y at %I:%M%p'


Then in a view f.e.

@user.date_of_birth.to_s :foomat
Time.now.to_s :foomat


alexwilliams posts on Dec 21, 2006 at 22:13
Thanks!

Important to restart the web server for that work :)

You need to create an account or log in to post comments to this site.


Click here to browse all 4834 code snippets

Related Posts