Almost every Rails project I start requires the use of custom formats for outputting Time. It's pretty trivial to add to a project, but I always forget the constant name for the initializer. It's Time::DATE_FORMATS. I inevitably have to google or look in previous projects. Not a big deal, but nonetheless, it's unnecessary.
This Gem provides some common formats as well allowing you to define your own in a date_time_formats.yml file.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install date_time_formats
DateTimeFormats adds support for common formats on the "Time#to_s" method
Time.now.to_s(:year) # => 2012 Time.now.to_s(:slash) # => 1/01/2012
DateTimeFormats comes with a default set of formats. You can define your own by adding a config/date_time_formats.yml file to your Rails app. The syntax is simple key value YAML.
# config/date_time_formats.yml year_and_day: "%Y-%d"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Added some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request